Scrapy管道spider_opened和spider_close没有被调用
我在使用 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_open
和 spider_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,我发帖后就发现了。您必须添加:
在
__init__
中,否则它永远不会收到调用它的信号Sorry, found it just after I posted this. You have to add:
in
__init__
otherwise it never receives the signal to call it正确的方法名称是
open_spider
和close_spider
,而不是spider_opened
和spider_close
。它记录在此处: http: //doc.scrapy.org/en/latest/topics/item-pipeline.html#writing-your-own-item-pipeline。Proper method names are
open_spider
andclose_spider
, notspider_opened
andspider_closed
. It is documented here: http://doc.scrapy.org/en/latest/topics/item-pipeline.html#writing-your-own-item-pipeline.