【问题标题】:Using xpath to get multiple selective values from within a single DIV class使用 xpath 从单个 DIV 类中获取多个选择性值
【发布时间】:2020-03-11 23:29:15
【问题描述】:

我正在使用 Xpath 使用 Python 抓取网站,并希望在

中返回多个元素
div class 

我已使用

缩小了相关信息的范围
//*[@class='category-product js-productitem']

这个选择在 Pastebin 上:https://pastebin.com/0A6LQ9DC

我希望能够从这一选择中提取多位信息。例如,“data-productid”、“data-price”、“data-specialprice”等

我想了解如何表示 Xpath 等价于 SQL

SELECT data-productid, data-price, data-specialprice FROM category-product js-productitem

在学习 Xpath 的同时,绝对/相对路径让我有些困惑。我假设如果我以这种方式(而不是相对路径)引用标签/名称的 唯一组合,我可以确保只获得我期望的信息,通过 Google Chrome 'inspect '函数。

【问题讨论】:

    标签: python xml xpath screen-scraping


    【解决方案1】:

    使用“根路径”和@ 属性符号来获取(get())你需要的东西。使用scrapy shell:

    response.xpath('//*[@class='category-product js-productitem']/@data-productid').get()
    response.xpath('//*[@class='category-product js-productitem']/@data-price').get()
    response.xpath('//*[@class='category-product js-productitem']/@data-specialprice').get()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2016-05-09
      • 2012-10-07
      相关资源
      最近更新 更多