Zia
Zia

Reputation: 404

What is output difference between selectors response.xpath and response.css?

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.

Upvotes: 1

Views: 908

Answers (1)

BoltClock
BoltClock

Reputation: 723829

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

Upvotes: 3

Related Questions