Python 2.7 scrapy:access spider类变量在管道\uu init中__
我知道您可以在process_item()中访问spider变量,但如何访问pipelineinit函数中的spider变量Python 2.7 scrapy:access spider类变量在管道\uu init中__,python-2.7,scrapy,Python 2.7,Scrapy,我知道您可以在process_item()中访问spider变量,但如何访问pipelineinit函数中的spider变量 class SiteSpider(CrawlSpider): def __init__(self): self.id = 10 class MyPipeline(object): def __init__(self): ... 我还需要访问MyPipeline中的自定义\u设置\u变量。您无法访问spider实例,因为管
class SiteSpider(CrawlSpider):
def __init__(self):
self.id = 10
class MyPipeline(object):
def __init__(self):
...
我还需要访问MyPipeline中的自定义\u设置\u变量。您无法访问spider实例,因为管道初始化是在引擎启动时完成的。事实上,您必须认为您的管道处理多个spider,而不仅仅是一个spider 话虽如此,您可以钩住
spider\u opened
信号,在spider实例启动时访问它
from scrapy import signals
class MyPipeline(object):
def __init__(self, mysetting):
# do stuff with the arguments...
self.mysetting = mysetting
@classmethod
def from_crawler(cls, crawler):
settings = crawler.settings
instance = cls(settings['CUSTOM_SETTINGS_VARIABLE']
crawler.signals.connect(instance.spider_opened, signal=signals.spider_opened)
return instance
def spider_opened(self, spider):
# do stuff with the spider: initialize resources, etc.
spider.log("[MyPipeline] Initializing resources for %s" % spider.name)
def process_item(self, item, spider):
return item
您无法访问spider实例,因为管道初始化是在引擎启动时完成的。事实上,您必须认为您的管道处理多个spider,而不仅仅是一个spider 话虽如此,您可以钩住
spider\u opened
信号,在spider实例启动时访问它
from scrapy import signals
class MyPipeline(object):
def __init__(self, mysetting):
# do stuff with the arguments...
self.mysetting = mysetting
@classmethod
def from_crawler(cls, crawler):
settings = crawler.settings
instance = cls(settings['CUSTOM_SETTINGS_VARIABLE']
crawler.signals.connect(instance.spider_opened, signal=signals.spider_opened)
return instance
def spider_opened(self, spider):
# do stuff with the spider: initialize resources, etc.
spider.log("[MyPipeline] Initializing resources for %s" % spider.name)
def process_item(self, item, spider):
return item
请更新您的答案:我还需要访问MyPipeline中的自定义\u设置\u变量。@hellomyfriends是设置模块中的设置吗?您可以通过
爬虫程序访问设置模块。设置。请更新您的答案:我还需要访问MyPipeline中的自定义\u设置\u变量。@hellomyfriends设置模块中的设置是什么?您可以通过爬虫程序访问设置模块。设置
。