Search code examples
xpathweb-scrapingscrapy

Extract first element with XPath and scrapy


I use .extract() to get the data from a xpath, like:

response.xpath('//*@id="bakery"]/span[2]/text()').extract()

the issue with this is that I always get a list as response. for example:

['23']

I only want the number, so I try with:

response.xpath('//*@id="bakery"]/span[2]/text()').extract()[0]

but this is a problem is the list empty, although I can use an exception to handle that scenario I guess there is a better way to do it


Solution

  • .extract_first() to the rescue:

    response.xpath('//*@id="bakery"]/span[2]/text()').extract_first()
    

    Instead of an exception, it would return None if no elements were matched.