1
votes

I am working in python to make crawlers with the help of Scrapy library. when i fetch data with selector response.xpath and response.css then it gives different result. like when i use xpath it does not show the result,if i replace the xpath with css then it shows the result. please help me to understand this concept.
xpath query

img = response.xpath('//div[@class="product-images"]//img/@src').extract()  

css query

img = response.css('div.product-images img::attr(src)').extract()  

Thanks.

1
Does the div.product-images element have more than one class? What does the HTML look like? - BoltClock
Yes it has more then one class.. <div class="product-images relative mb-half has-hover woocommerce-product-gallery woocommerce-product-gallery--with-images woocommerce-product-gallery--columns-4 images"> - Zia

1 Answers

3
votes

The XPath predicate [@class="product-images"] performs an exact match on the class attribute value, which means it will only match an element with class="product-images". If the element has more than one class, it will not be matched by the predicate. A class selector on the other hand will match an element with the specified class name even if it has more than one class.

The XPath equivalent to a class selector that accounts for multiple classes is rather cumbersome, since XPath doesn't have a function designed for this specific purpose:

img = response.xpath('//div[contains(concat(" ", @class, " "), " product-images ")]//img/@src').extract()