gpt4 book ai didi

python - Scrapy:跳过项目并继续执行

转载 作者:太空狗 更新时间:2023-10-30 00:30:35 24 4
gpt4 key购买 nike

我正在做一个 RSS 蜘蛛。我想继续执行如果当前没有匹配项,蜘蛛将忽略当前节点item...到目前为止我得到了这个:

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

(信息是之前从 xpath 中清理过的字符串...)

但是我遇到了这个异常:

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

蜘蛛

那么如何忽略这个节点并继续执行呢?

最佳答案

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

但最好不要使用 return,使用 yield 或者什么都不做

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

关于python - Scrapy:跳过项目并继续执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com