Search code examples
shellxpathscrapy

How to extract the value of an HTML attribute using scrapy response.xpath?


I'm trying to extract the value of the attribute data-asin-price inside a <div> tag

Which in the example below you can see is 22.63

<div id="cerberus-data-metrics" style="display: none;" data-asin="B079GMRZ8S" data-asin-price="22.63" data-asin-shipping="0.0" data-asin-currency-code="AUD" data-substitute-count="-1" data-device-type="WEB" data-display-code="Asin is not eligible because it is not enabled"></div>

Is there any way to do this using response.xpath() with scrapy?

Thank you


Solution

  • I just wanted to post the answer I found.

    To get the 22.63 value our of the data-asin-price attribute in scrapy shell I did the following:

    response.xpath('//div[@id = "cerberus-data-metrics"]/@data-asin-price').extract_first()
    

    Cheers