使用xpath从单个DIV类中获取多个选择性值



我正在使用Xpath用Python抓取一个网站,并希望在中返回多个元素

div class 

我已经使用缩小了有问题的信息范围

//*[@class='category-product js-productitem']

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

我希望能够从这个选择中提取多个信息。例如,"数据生产"、"数据价格"、"特定数据价格"等

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

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

在学习Xpath时,绝对/相对路径让我有些困惑。我假设,如果我以这种方式(而不是相对路径(引用标签/名称的唯一组合,我可以确保只获得我期望的信息,并通过谷歌Chromes的"检查"功能进行了检查。

使用"根路径"和@attribute符号来获取(get(((所需内容。外壳有碎屑:

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

最新更新