gpt4 book ai didi

python - scrapy xpath选择器可以在浏览器中使用,但不能在crawl或shell中使用

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:06 26 4
gpt4 key购买 nike

我正在抓取以下页面:http://www.worldfootball.net/all_matches/eng-premier-league-2015-2016/

第一次解析将完成并且应该得到所有带有分数作为文本的链接。我首先循环遍历所有匹配行:

for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):

然后获取表格第6列的链接

    matchHref = sel.xpath('.//td[6]/a/@href').extract()

但是这不会返回任何内容。我在 Chrome 中尝试了相同的选择器(在 table 和 tr 选择器之间添加了“tbody”),并且得到了结果。但是,如果我在 scrapy shell 中尝试相同的选择器(不带 tbody),我只能从第一个 response.xpath 获得结果,而以下链接提取则没有任何结果。

我以前做过一些这样的循环,但这个简单的事情让我难住了。有更好的方法来调试这个吗?这是一些 shell 输出,我只是尝试简化第二个选择以仅选择任何 td

In [36]: for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
....: sel.xpath('.//td')
....:

什么都没有。有想法吗?

最佳答案

我要做的是利用第六列中的这些链接在 href 属性值中包含 report 的事实。来自 shell 的演示:

$ scrapy shell "http://www.worldfootball.net/all_matches/eng-premier-league-2015-2016/"
>>> for row in response.xpath('(//table[@class="standard_tabelle"])[1]/tr[not(th)]'):
... print(row.xpath(".//a[contains(@href, 'report')]/@href").extract_first())
...
/report/premier-league-2015-2016-manchester-united-tottenham-hotspur/
/report/premier-league-2015-2016-afc-bournemouth-aston-villa/
/report/premier-league-2015-2016-everton-fc-watford-fc/
...
/report/premier-league-2015-2016-stoke-city-west-ham-united/
/report/premier-league-2015-2016-swansea-city-manchester-city/
/report/premier-league-2015-2016-watford-fc-sunderland-afc/
/report/premier-league-2015-2016-west-bromwich-albion-liverpool-fc/

另请注意这部分:tr[not(th)] - 这有助于跳过没有相关链接的标题行。

关于python - scrapy xpath选择器可以在浏览器中使用,但不能在crawl或shell中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755573/

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