scrapy xpath选择器在浏览器中工作,但在crawl或sh中不起作用

2024-09-28 23:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

第一个解析通过,应该得到所有的链接,分数作为文本。我首先遍历所有匹配行:

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

然后得到表格第6列的链接

^{pr2}$

但这不会返回任何结果。不过,我在Chrome中尝试了相同的选择器(在table和tr selector之间添加了“tbody”),得到了结果。但是,如果我在scrapy shell中尝试相同的选择器(没有tbody),我只会从第一个选择器得到结果响应.xpath,而没有用下面的链接提取。在

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

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

没什么。思想?在


Tags: infor链接responsetable选择器shellxpath
1条回答
网友
1楼 · 发布于 2024-09-28 23:38:24

我要做的是使用第6列中的这些链接在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)]-这有助于跳过没有相关链接的标题行。在

相关问题 更多 >