我正在尝试在Scrapy扩展程序“MyExtension”中处理自定义信号“ signalizers.item_extracted ”,该扩展程序在scrapy启动时成功启用。这是我的代码:
signalizers.py
# custom signals
item_extracted = object()
item_transformed = object()
class MyExtension(object):
def __init__(self):
pass
@classmethod
def from_crawler(cls, crawler):
# first check if the extension should be enabled and raise
# NotConfigured otherwise
if not crawler.settings.getbool('MYEXTENSION_ENABLED'):
raise NotConfigured
# instantiate the extension object
ext = cls()
# connect the extension object to signals
crawler.signals.connect(ext.item_extracted, signal=item_extracted)
# return the extension object
return ext
def item_extracted(self, item, spider):
#Do some stuff
然后我尝试发送' signalizers.item_extracted '信号,但我认为没有处理,或者至少我不能看到实际输出也不能调试它:
在蜘蛛中:
SignalManager(dispatcher.Any).send_catch_log(
signal=signalizers.item_extracted,
item=item,
spider=spider)
我在这里做错了吗?
答案 0 :(得分:0)
在阅读了一些Scrapy的源代码后,我发现问题在于创建一个新的Signal Manager实例而不是使用crawler的实例:
spider.crawler.signals.send_catch_log(signal=signalizers.item_extracted, item=item, spider=spider)
现在扩展
正确处理