中间件是scrapy的核心部分,数据的传递,队列的维护,都是靠中间件,所以了解他的生命周期还是很有必要的 https://github.com/scrapy/scrapy/blob/master/scrapy/core/spidermw.py def _add_middleware(self, mw): super(SpiderMiddlewareManager, self)._add_middleware(mw) if hasattr(mw, 'process_spider_input'): self.methods['process_spider_input'].append(mw.process_spider_input) if hasattr(mw, 'process_spider_output'): self.methods['process_spider_output'].insert(0, mw.process_spider_output) if hasattr(mw, 'process_spider_exception'): self.methods['process_spider_exception'].insert(0, mw.process_spider_exception) if hasattr(mw, 'process_start_requests'): self.methods['process_start_requests'].insert(0, mw.process_start_requests) 将中间件的每内置的方法加入到方法链上,注意,这里的数值越小,处理越靠前,下同 https://github.com/scrapy/scrapy/blob/master/scrapy/core/scraper.py 这里就去处理spiderMiddleWare class Scraper(object):……
阅读全文