Scrapy You cannot return an “NoneType” object from a

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: Scrapy You cannot return an “NoneType” object from a

scrapy中,当进入到parse函数中,假如有条件不满足,希望爬虫跳过此次解析,如下代码是有错误的:

if info.startswith('Foo'):
            item['foo'] = info.split(':')[1]
        else:
            return None

Scrapy中spider不允许返回None,如果返回None,将会报错:

exceptions.TypeError: You cannot return an "NoneType" object from a spider

正确的做法:

parse(response):
    #make some manipulations
    if info.startswith('Foo'):
            item['foo'] = info.split(':')[1]
            return [item]
        else:
            return []

或者什么也不做:

parse(response):
    #make some manipulations
    if info.startswith('Foo'):
            item['foo'] = info.split(':')[1]
            yield item
        else:
            return
原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: Scrapy You cannot return an “NoneType” object from a

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: