Scrapy选择器返回None时如何设置默认值 [英] How to set a default value when Scrapy selector returns None

查看:43
本文介绍了Scrapy选择器返回None时如何设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 xpath 选择器的结果返回 None 时,我试图设置默认值.当在某些页面中 xpath 节点不存在并且我想设置例如N/A"或未找到"时,就会发生这种情况.

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"

有什么想法吗?谢谢

推荐答案

没有必要进行两次查询,一个简单的解决方案是传递一个默认值:

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

为了将来参考,如果您要在不使用 default 关键字的情况下重写自己的代码,我会查询一次并使用 if/else:

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"

这篇关于Scrapy选择器返回None时如何设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆