无法从xpath响应输出特定行

时间:2015-12-30 02:44:38

标签: python xpath scrapy

这是我第一次尝试使用xpath

我试图通过Python上的Scrapy使用xpath接口提取列表中产品的信息,特别是来自此网址的价格:

http://store.nike.com/gb/en_gb/pw/mens-shoes/7puZoi3?ipp=120#

正如您所看到的(价格从左到右水平)是90英镑,120英镑, 100英镑......

以下内容将返回页面上所有培训师价格的列表:

item ['trainerPrice'] = response.xpath('// span [@ class =“local nsg-font-family - base”] / text()')。extract() enter image description here

更重要的是,以下内容将返回第一个“记录”:

item ['trainerPrice'] = response.xpath('string(// span [@ class =“local nsg-font-family - base”] / text())')。extract()

enter image description here

但我不确定如何选择第二条记录,即u'\ xa3120'

任何提示?

1 个答案:

答案 0 :(得分:2)

您可以从提取的列表中获取第二项:

prices = response.xpath('//span[@class="local nsg-font-family--base"]/text()').extract() 
print(prices[1])

尽管如此,我并不特别喜欢你的定位器。相反,我会依赖prices div:

response.css('div.prices span.local::text')