如果item['business_name']
等于此[]
或None
。我想从查询结果中删除它。
相反,它输出这是我不想要的,我只想要具有商家名称的查询结果
'business_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
答案 0 :(得分:2)
你可以尝试:
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”的方式。
答案 1 :(得分:1)
pythonic解决方案
if not item['business_name']:
Do something
因为None和空列表都具有布尔值false