Skip to main content
 首页 » 编程设计

python之无法在python scrapy中运行扩展

2024年10月25日11zhenyulu

我的 craigscrap/extension.py 是这样的:

from scrapy import signals 
from scrapy.exceptions import NotConfigured 
 
class SendEmail(object): 
 
    def __init__(self): 
        self.frm = 'wrufesh@gmail.com' 
        self.to = 'a@f.com, b@g.com' 
 
    @classmethod 
    def from_crawler(cls, crawler): 
        if not crawler.settings.getbool('MYEXT_ENABLED'): 
            raise NotConfigured 
        ext = cls() 
        crawler.signals.connect(ext.send_mail, signal=signals.spider_closed) 
        return ext 
 
    def send_mail(self, spider): 
        spider.log("Are you getting the message?") 

我的设置是这样的:

EXTENSIONS = { 
    'craigscrap.extension.SendEmail': 80, 
} 

我正在阅读实现扩展的文档。我为扩展编写了这段代码,但它似乎没有运行。正如我希望打印此代码

"Are you getting the message?" 

蜘蛛关闭时。 请帮我找出我的错误。

请您参考如下方法:

我发现了我的错误。 在 settings.py 中添加以下变量

MYEXT_ENABLED = True     
 
EXTENSIONS = { 
'craigscrap.extension.SendEmail': 80, 
}