例如:
class CookiesMiddleware(object):
"""This middleware enables working with sites that need cookies"""
def __init__(self, debug=False):
self.jars = defaultdict(CookieJar)
self.debug = debug
@classmethod
def from_crawler(cls, crawler):
if not crawler.settings.getbool('COOKIES_ENABLED'):
raise NotConfigured
return cls(crawler.settings.getbool('COOKIES_DEBUG'))
为什么不可以直接在init中使用 crawler 进行初始化呢,这样做有什么好处?
1
rebornix 2016-02-21 18:07:20 +08:00
你看下初始化 middleware 的代码 https://github.com/scrapy/scrapy/blob/6660175de9abddaf29be2b5f74e3f04a389618bf/scrapy/middleware.py#L33
mwcls = load_object(clspath) if crawler and hasattr(mwcls, 'from_crawler'): mw = mwcls.from_crawler(crawler) elif hasattr(mwcls, 'from_settings'): mw = mwcls.from_settings(settings) else: mw = mwcls() middlewares.append(mw) |