您無法訪問蜘蛛實例,因爲在引擎啓動時完成了管道初始化。事實上,你必須認爲你的管道可以處理多個蜘蛛,而不是一個蜘蛛。
話雖如此,你可以掛鉤spider_opened
信號來訪問蜘蛛實例啓動時。
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中的CUSTOM_SETTINGS_VARIABLE。 –
@hellomyfriends是設置模塊中的設置嗎?您可以通過'crawler.settings'訪問設置模塊。 – Rolando
是的。非常感謝 –