Python Scrapy:跳过项目并继续执行
我正在做一个RSS蜘蛛。我想继续执行 如果当前节点中没有匹配项,spider将忽略当前节点 项目。。。到目前为止,我得到了这个:Python Scrapy:跳过项目并继续执行,python,web-crawler,scrapy,Python,Web Crawler,Scrapy,我正在做一个RSS蜘蛛。我想继续执行 如果当前节点中没有匹配项,spider将忽略当前节点 项目。。。到目前为止,我得到了这个: if info.startswith('Foo'): item['foo'] = info.split(':')[1] else: return None (info是一个字符串,在…)之前已从xpath中清除) 但我有一个例外: exceptions.TypeError: Yo
if info.startswith('Foo'):
item['foo'] = info.split(':')[1]
else:
return None
(info是一个字符串,在…)之前已从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 []
但最好不要使用收益,使用收益率,或者什么都不做
parse(response):
#make some manipulations
if info.startswith('Foo'):
item['foo'] = info.split(':')[1]
yield item
else:
return
当我不得不在解析过程中跳过该项,但在回调函数之外时,我发现了一个未记录的方法 只需在解析过程中的任意位置提升
StopIteration
class MySpider(Spider):
def parse(self, response):
value1 = parse_something1()
value2 = parse_something1()
yield Item(value1, value2)
def parse_something1(self):
try:
return get_some_value()
except Exception:
self.skip_item()
def parse_something2(self):
if something_wrong:
self.skip_item()
def skip_item(self):
raise StopIteration