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

问题描述 投票:0回答:1

[我正在使用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,但绝对/相对路径使我有些困惑。我假设如果我以这种方式(而不是相对路径)引用标记/名称的[[unique组合,则可以通过Google Chromes的“检查”功能检查得到的信息,从而确保只获取期望的信息。

python xml xpath screen-scraping
1个回答
0
投票
使用“根路径”和@属性符号来获取(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()

© www.soinside.com 2019 - 2024. All rights reserved.