middlewares.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Define here the models for your spider middleware
  3. #
  4. # See documentation in:
  5. # http://doc.scrapy.org/en/latest/topics/spider-middleware.html
  6. from scrapy import signals
  7. class ShlibSpiderMiddleware(object):
  8. # Not all methods need to be defined. If a method is not defined,
  9. # scrapy acts as if the spider middleware does not modify the
  10. # passed objects.
  11. @classmethod
  12. def from_crawler(cls, crawler):
  13. # This method is used by Scrapy to create your spiders.
  14. s = cls()
  15. crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
  16. return s
  17. def process_spider_input(self, response, spider):
  18. # Called for each response that goes through the spider
  19. # middleware and into the spider.
  20. # Should return None or raise an exception.
  21. return None
  22. def process_spider_output(self, response, result, spider):
  23. # Called with the results returned from the Spider, after
  24. # it has processed the response.
  25. # Must return an iterable of Request, dict or Item objects.
  26. for i in result:
  27. yield i
  28. def process_spider_exception(self, response, exception, spider):
  29. # Called when a spider or process_spider_input() method
  30. # (from other spider middleware) raises an exception.
  31. # Should return either None or an iterable of Response, dict
  32. # or Item objects.
  33. pass
  34. def process_start_requests(self, start_requests, spider):
  35. # Called with the start requests of the spider, and works
  36. # similarly to the process_spider_output() method, except
  37. # that it doesn’t have a response associated.
  38. # Must return only requests (not items).
  39. for r in start_requests:
  40. yield r
  41. def spider_opened(self, spider):
  42. spider.logger.info('Spider opened: %s' % spider.name)