gpt4 book ai didi

python-3.x - XPath表达式在scrapy中返回空列表

转载 作者:行者123 更新时间:2023-12-03 16:04:44 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?

(2 个回答)


4年前关闭。




我在抓取http://stats.espncricinfo.com/ci/engine/records/index.html?id=2;type=team

我需要的是附加到 XPath 表达式的链接

/html/body/div[1]/div[3]/div[4]/table/tbody/tr/td[1]/div[2]/table[1]/tbody/tr/td/ul[2]/li/a[2]

在页面中它是由 "One-day Internationals" 标记的元素在按年份排列的比赛结果列表下。上面的表达式是使用 Firefox 扩展 Firebug 获得的。

但是它返回一个空列表。尝试过使用替代的 xpath 表达式,例如
//div[@id="ciHomeContentlhs"]/table/tbody/tr/td[1]/div/table[2]/tbody/tr/td/ul/li/a[2]/@href

结果相同。

Xpath 表达式
//div[@id="ciHomeContentlhs"]/table

正在给我 table 。然而
//div[@id="ciHomeContentlhs"]/table/tbody

正在返回一个空列表。我已经测试了 xpath 表达式
http://videlibri.sourceforge.net/cgi-bin/xidelcgi它显示了所需的 href或节点作为输出。我似乎无法在 Python 上类.

最佳答案

<tbody>元素不是初始 HTML 的一部分source - 它是由浏览器解析器生成的,所以你不应该在你的 XPath 中使用它表达。

您可以使用链接文本来匹配确切的元素:

//a[text()="One-Day Internationals"]

关于python-3.x - XPath表达式在scrapy中返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com