Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value when Scrapy selector returns None

I was trying to set default value when the result of my xpath selector return None. This happens when in some pages the xpath node dont exist and I want to set for example 'N/A' or 'Not found'.

I used the following code but I think this isn't clean and efficient:

value = response.xpath(property.xpath).extract_first()

if(value != None):
    data[property.name] = response.xpath(property.xpath).extract_first()
else:
    data[property.name] = "N/A"

Any ideas? Thanks

like image 557
Deoxyseia Avatar asked Dec 11 '22 16:12

Deoxyseia


1 Answers

There is no need to do the query twice, a simple solution is to pass a default value:

data[property.name] = response.xpath(property.xpath).extract_first(default='N/A')

For future reference, if you were to rewrite your own code without using the default keyword, I would query once and use an if/else:

value = response.xpath(property.xpath).extract_first()
data[property.name] = value if value else "N/A"
like image 103
Padraic Cunningham Avatar answered Jan 01 '23 10:01

Padraic Cunningham