How to set a default value when Scrapy selector returns None

前端 未结 1 1939
灰色年华
灰色年华 2021-01-06 06:06

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 \

相关标签:
1条回答
  • 2021-01-06 06:11

    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"
    
    0 讨论(0)
提交回复
热议问题