【问题标题】:not able to output specific row from xpath response无法从 xpath 响应中输出特定行
【发布时间】:2016-04-03 23:57:07
【问题描述】:

这是我第一次尝试使用 xpath

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

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()

更是如此,以下将返回第一个“记录”:

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

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

有什么提示吗?

【问题讨论】:

    标签: python xpath scrapy


    【解决方案1】:

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

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

    不过,我不是特别喜欢你的定位器。相反,我会依赖 prices div:

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

    【讨论】:

    • 谢谢!我没有正确思考,也没有索引我的数组变量
    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多