如何在python scrapy中检查项是否为None
如果如何在python scrapy中检查项是否为None,python,scrapy,Python,Scrapy,如果项['business\u name']等于此[]或无。我想从查询结果中删除它 相反,它会输出我不想要的结果,我只想要具有业务名称的查询结果 “业务名称”:[], 这就是我目前所拥有的 class Item(scrapy.Item): business_name = scrapy.Field() website = scrapy.Field() phone_number = scrapy.Field() class QuotesSpider(scrapy.Spide
项['business\u name']
等于此[]
或无
。我想从查询结果中删除它
相反,它会输出我不想要的结果,我只想要具有业务名称的查询结果
“业务名称”:[],
这就是我目前所拥有的
class Item(scrapy.Item):
business_name = scrapy.Field()
website = scrapy.Field()
phone_number = scrapy.Field()
class QuotesSpider(scrapy.Spider):
def parse(self, response):
for business in response.css('div.info'):
item = Item()
item['business_name'] = business.css('span[itemprop="name"]::text').extract()
if item['business_name'] is None :
break
else:
item['website'] = business.css('div.links a::attr(href)').extract_first()
item['phone_number'] = business.css('div.phones.phone.primary::text').extract()
yield item
你可以试试:
if item['business_name'] is None or len(item['business_name']) == 0:
# delete it here
或者把你的逻辑反过来:
if item['business_name']:
item['website'] = business.css('div.links a::attr(href)').extract_first()
item['phone_number'] = business.css('div.phones.phone.primary::text').extract()
yield item
后者利用了
None
和Python
中的空列表“falsy”,被认为是更“Pythonic”的方式
if not item['business_name']:
Do something
因为None和空列表都有布尔值
false
,您能更好地解释一下为什么您的方法有效,而mines没有一个更简单的解释:)谢谢,还有一种方法可以检查是否存在重复项,比如如果业务名称已经存在,请从查询中删除,知道我的意思吗?他们有一个重复项过滤器,看见