XPath 选择器在 XPath Helper 控制台中工作,但在 scrapy 中不起作用

XPath selector works in XPath Helper console, but doesn't work in scrapy

我正在使用 scrapy 解析来自 Russian Central Bank website

的利率

我还在 Google Chrome 中使用 Xpath Helper 扩展来查找必要的 XPath 选择器。我在下面的 XPath Helper Console 中使用的选择器完全符合我的需要。

出于某种原因,相同的查询在我的蜘蛛中不起作用,即使它导航到该页面也是如此。

你可以在下面看到我的蜘蛛代码。

import scrapy
import urllib.parse

class RatesSpider(scrapy.Spider):
   name = 'rates'
   allowed_domains = ['cbr.ru']
   start_urls = ['https://www.cbr.ru/hd_base/zcyc_params/zcyc/?DateTo=01.10.2018']

   def parse(self, response):

    rates = response.xpath('/html/body/div/div/div/div/div/table/tbody/tr[2]/td').extract()

    yield {'Rates': rates
       }

该页面似乎没有被登录阻止,因为我可以解析页面上的其他元素。

我该怎么做才能使我的代码正常工作?

Table 不包含那个 tbody 节点 - 它是在呈现页面时由浏览器添加的,所以 不要在 XPath 中使用它( .../table/tbody/tr/... -> .../table//tr/...):

rates = response.xpath('/html/body/div/div/div/div/div/table//tr[2]/td').extract()

或简化

rates = response.xpath('//td').extract()