Home > OS >  Get value of "data-..." attribute with .css selector with Scrapy
Get value of "data-..." attribute with .css selector with Scrapy

Time:05-30

I am trying to get the value of a data-attribute with scrapy:

response.css('.product-header-top div::attr("data-background-image")').get()

But I do not get the value of data-background-image and Python throws an error:

raise SelectorSyntaxError(cssselect.parser.SelectorSyntaxError: Got pseudo-element ::FunctionalPseudoElement[::attr(['data-background-image'])] not at the end of a selector

Here is the relevant HTML Code of the webpage:

<div data-background-image="/images/image.jpg" style="background-image: url(&quot;/images/image.jpg&quot;);"></div>

Thanks

UPDATE F.Hoque is right and it works fine. The website is dynamic and renders the data-background-image with JS. So the ::attr("data-...") is working. Thanks for your help @F.Hoque!

CodePudding user response:

Your CSS selection is working fine. There is a typo ); just remove it.

response.css('.product-header-top div::attr("data-background-image")').get()

Proven by Scrapy shell:

In [26]:  sel.css('div::attr("data-background-image")').get()
Out[26]: '/images/image.jpg'
  • Related