Scrappy xPath根据值搜索同一节点

时间:2017-07-07 02:28:37

标签: html xpath scrapy

任何想法如何提取" sku"基于此值," configId":" 785877"
感谢

response.xpath('//div[@class="c-quick-buy  c-product-card__buy-button"]/@data-js-component-params').extract()

数据提取

<div class="c-quick-buy  c-product-card__buy-button"

        data-js-component="quick-buy"
        data-js-component-params='{
            "product": {
                "sku": "NO225ELAQUDXANMY",
                "skuSimple": "NO225ELAQUDXANMY-799443",
                "price": "90.00",
                "configId": "785877",
                "ff": "1"
            },
            "hasSize": false
        }'
     >

1 个答案:

答案 0 :(得分:1)

根据内部json的可预测程度,您可以执行以下操作:

xpath_query = '//div[contains(@data-js-component-params, \'"configId": "785877"\')]/@data-js-component-params'
js_data = response.xpath(xpath_query).extract_first() or '{}'
json.loads(js_data).get('product',{}).get('sku')

您甚至可以将查询更改为

xpath_query = '//div[re:test(@data-js-component-params, \'"configId":\s+"785877"\')]/@data-js-component-params'