Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 366
Luis Ramon Ramirez Rodriguez Avatar asked Dec 15 '22 05:12

Luis Ramon Ramirez Rodriguez


2 Answers

.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.

like image 150
alecxe Avatar answered Jan 01 '23 10:01

alecxe


There is a new Scrapy built in method get() can be used instead of extract_first() which always returns a string and None if no element exists.

response.xpath('//*@id="bakery"]/span[2]/text()').get()
like image 42
Moein Kameli Avatar answered Jan 01 '23 09:01

Moein Kameli