lxml 返回空列表



我在互联网上看到过很多解决方案,但似乎都不起作用。

我有以下代码可以从 Imdb 中的用户那里检索信息:

from lxml import html
import requests
page = requests.get('http://www.imdb.com/user/ur6447592/comments-expanded?start=0&order=alpha')
tree = html.fromstring(page.content)
result = tree.xpath('//*[@id="outerbody"]/tbody/tr/td/b[2]/text()')
print(result)

输出应为:

["Little flesh and all bones"]

将 xpath 参数更改为:

'//*[@id="outerbody"]/tr/td/b[2]/text()'

编辑:

多亏了评论,我才意识到为什么OP会遇到这个问题。

您可以打印page.content以查看原始 html。(通过@JacobIRR(

或者,在 Firefox 中,工具 - Web 开发人员 - 页面源代码。

在Google Chrome Developer Tools中,如@corn3lius引用:

如果您使用网络选项卡并查看返回的文档,它将 在谁惹 DOM 之前给你原始状态。

最新更新