BARNOWL
BARNOWL

Reputation: 3589

how to check if item is None in python scrapy

If item['business_name'] is equal to this [] or None. I want to delete it from query results.

Instead it outputs this which is what i don't want, i only want query results that have a business name

'business_name': [],

this is what i have so far

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

Upvotes: 3

Views: 2553

Answers (2)

Jan
Jan

Reputation: 43169

You could try:

if item['business_name'] is None or len(item['business_name']) == 0:
    # delete it here

Or turn your logic the other way round:

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

The latter makes use of None and an empty list being "falsy" in Python and is considered the more "Pythonic" way.

Upvotes: 5

Mia
Mia

Reputation: 2676

A pythonic solution would be

if not item['business_name']: 
    Do something

Because both None and an empty list have boolean value of false

Upvotes: 1

Related Questions