我有一个脚本可以从亚马逊获取图书信息,该脚本之前运行成功,但今天失败了。我无法弄清楚到底出了什么问题,但我假设它与解析器或 Javascript 相关。我正在使用下面的代码。
from bs4 import BeautifulSoup
import requests
response = requests.get('https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=9780307397980',headers={'User-Agent': b'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'})
html = response.content
soup = BeautifulSoup(html, "html.parser")
resultcol = soup.find('div', attrs={'id':'resultsCol'})
之前我是在resultcol
中获取数据的但现在它是空白的。当我检查html
时我看到我正在寻找的标签,即 <div id="resultsCol" class=\'\' >
。但是soup
里面没有这段文字。谁能帮我调试这个吗?以前工作得很好,但现在不行了。
最佳答案
删除标题,它应该可以工作。
from bs4 import BeautifulSoup
import requests
response = requests.get('https://www.amazon.com/s/ref=nb_sb_noss?url=search- alias%3Dstripbooks&field-keywords=9780307397980')
html = response.content
soup = BeautifulSoup(html, "html.parser")
resultcol = soup.find('div', attrs={'id':'resultsCol'})`
关于javascript - Python BeautifulSoup html.parser 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52304522/