Rick
Rick

Reputation: 541

Scrappy xPath search the same node based on value

Any idea how to extract the value of "sku" based on this value, "configId": "785877"
Thanks

Scrape

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

Data 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
        }'
     >

Upvotes: 0

Views: 38

Answers (1)

Wilfredo
Wilfredo

Reputation: 1548

Depending on how predictable is the inner json you could do something like:

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

you could even change the the query to

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

Upvotes: 1

Related Questions