我对使用 Python 进行抓取还很陌生。 我正在尝试从 Exilead 上的查询中获取搜索结果的数量。在此example我想得到“ 586,564 个结果”。
这是我正在运行的代码:
r = requests.get(URL, headers=headers)
tree = html.fromstring(r.text)
stats = tree.xpath('//[@id="searchform"]/div/div/small/text()')
这将返回一个空列表。
我直接从元素页面复制粘贴 xPath。
作为替代方案,我尝试使用 BeautifulSoup :
html = r.text
soup = BeautifulSoup(html, 'xml')
stats = soup.find('small', {'class': 'pull-right'}).text
返回属性错误:NoneType 对象没有属性文本。
当我检查 html 源代码时,我意识到我实际上无法在源代码中找到我要查找的元素(结果数)。
有谁知道为什么会发生这种情况以及如何解决这个问题? 非常感谢!
最佳答案
When I checked the html source I realised I actually cannot find the element I am looking for (the number of results) on the source.
这表明您正在查找的数据是使用 JavaScript 动态生成的。您需要能够在 html 源代码中看到您要查找的元素。
要确认这是导致错误的原因,您可以尝试一些非常简单的操作,例如:
html = r.text
soup = BeautifulSoup(html, 'lxml')
*注意上面的“lxml”。
然后手动检查“汤”以查看是否存在您想要的元素。
关于python xpath 返回空列表 - exilead,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53309097/