scrapy xpath 选择器在浏览器中有效,但在抓取或 shell 中无效
scrapy xpath selector works in browser, but not in crawl or shell
我正在抓取以下页面:http://www.worldfootball.net/all_matches/eng-premier-league-2015-2016/
第一次解析通过,应该得到所有带有分数的 link 作为文本。我首先遍历所有匹配行:
for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
然后得到table
第6列的link
matchHref = sel.xpath('.//td[6]/a/@href').extract()
这不过returns没什么。我在 Chrome 中尝试了相同的 selector(在 table 和 tr selector 之间添加了 'tbody'),但我得到了结果。但是,如果我在 scrapy shell 中尝试相同的 select 或(没有 tbody),我只会从第一个 response.xpath 中获得结果,而以下 link 提取则没有结果.
我以前做过一些这样的循环,但这个简单的事情让我感到难过。有没有更好的方法来调试这个?这是一些 shell 输出,我只是尝试将第二个 selection 简化为 select any td
In [36]: for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
....: sel.xpath('.//td')
....:
没有。想法?
我要做的是利用第 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)]
- 这有助于跳过没有相关链接的 header 行。
我正在抓取以下页面:http://www.worldfootball.net/all_matches/eng-premier-league-2015-2016/
第一次解析通过,应该得到所有带有分数的 link 作为文本。我首先遍历所有匹配行:
for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
然后得到table
第6列的link matchHref = sel.xpath('.//td[6]/a/@href').extract()
这不过returns没什么。我在 Chrome 中尝试了相同的 selector(在 table 和 tr selector 之间添加了 'tbody'),但我得到了结果。但是,如果我在 scrapy shell 中尝试相同的 select 或(没有 tbody),我只会从第一个 response.xpath 中获得结果,而以下 link 提取则没有结果.
我以前做过一些这样的循环,但这个简单的事情让我感到难过。有没有更好的方法来调试这个?这是一些 shell 输出,我只是尝试将第二个 selection 简化为 select any td
In [36]: for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
....: sel.xpath('.//td')
....:
没有。想法?
我要做的是利用第 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)]
- 这有助于跳过没有相关链接的 header 行。