Scrapy管道spider_opened和spider_close没有被调用

发布于 2024-09-30 17:51:05 字数 842 浏览 4 评论 0原文

我在使用 scrapy 管道时遇到了一些麻烦。我的信息正在从网站上正常抓取,并且 process_item 方法正在被正确调用。然而,spider_opened 和spider_close 方法没有被调用。

class MyPipeline(object):

    def __init__(self):
        log.msg("Initializing Pipeline")
        self.conn = None
        self.cur = None

    def spider_opened(self, spider):
        log.msg("Pipeline.spider_opened called", level=log.DEBUG)

    def spider_closed(self, spider):
        log.msg("Pipeline.spider_closed called", level=log.DEBUG)

    def process_item(self, item, spider):
        log.msg("Processsing item " + item['title'], level=log.DEBUG)

__init__process_item 日志消息都显示在日志中,但 spider_openspider_close 日志消息不显示。

我需要使用 Spider_opened 和 Spider_Closed 方法,因为我想使用它们来打开和关闭与数据库的连接,但日志中没有显示任何内容。

如果有人有任何建议,那将非常有用。

I am having some trouble with a scrapy pipeline. My information is being scraped form sites ok and the process_item method is being called correctly. However the spider_opened and spider_closed methods are not being called.

class MyPipeline(object):

    def __init__(self):
        log.msg("Initializing Pipeline")
        self.conn = None
        self.cur = None

    def spider_opened(self, spider):
        log.msg("Pipeline.spider_opened called", level=log.DEBUG)

    def spider_closed(self, spider):
        log.msg("Pipeline.spider_closed called", level=log.DEBUG)

    def process_item(self, item, spider):
        log.msg("Processsing item " + item['title'], level=log.DEBUG)

Both the __init__ and process_item logging messages are displyed in the log, but the spider_open and spider_close logging messages are not.

I need to use the spider_opened and spider_closed methods as I want to use them to open and close a connection to a database, but nothing is showing up in the log for them.

If anyone has any suggested that would be very useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-10-07 17:51:05

抱歉,我发帖后就发现了。您必须添加:

dispatcher.connect(self.spider_opened, signals.spider_opened)
dispatcher.connect(self.spider_closed, signals.spider_closed)

__init__ 中,否则它永远不会收到调用它的信号

Sorry, found it just after I posted this. You have to add:

dispatcher.connect(self.spider_opened, signals.spider_opened)
dispatcher.connect(self.spider_closed, signals.spider_closed)

in __init__ otherwise it never receives the signal to call it

远昼 2024-10-07 17:51:05

正确的方法名称是 open_spiderclose_spider,而不是 spider_openedspider_close。它记录在此处: http: //doc.scrapy.org/en/latest/topics/item-pipeline.html#writing-your-own-item-pipeline

Proper method names are open_spider and close_spider, not spider_opened and spider_closed. It is documented here: http://doc.scrapy.org/en/latest/topics/item-pipeline.html#writing-your-own-item-pipeline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文