init.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. from scrapy.spiders import Spider
  2. from scrapy.utils.spider import iterate_spider_output
  3. class InitSpider(Spider):
  4. """Base Spider with initialization facilities"""
  5. def start_requests(self):
  6. self._postinit_reqs = super(InitSpider, self).start_requests()
  7. return iterate_spider_output(self.init_request())
  8. def initialized(self, response=None):
  9. """This method must be set as the callback of your last initialization
  10. request. See self.init_request() docstring for more info.
  11. """
  12. return self.__dict__.pop('_postinit_reqs')
  13. def init_request(self):
  14. """This function should return one initialization request, with the
  15. self.initialized method as callback. When the self.initialized method
  16. is called this spider is considered initialized. If you need to perform
  17. several requests for initializing your spider, you can do so by using
  18. different callbacks. The only requirement is that the final callback
  19. (of the last initialization request) must be self.initialized.
  20. The default implementation calls self.initialized immediately, and
  21. means that no initialization is needed. This method should be
  22. overridden only when you need to perform requests to initialize your
  23. spider
  24. """
  25. return self.initialized()