middlewares.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. # Define here the models for your spider middleware
  3. #
  4. # See documentation in:
  5. # https://doc.scrapy.org/en/latest/topics/spider-middleware.html
  6. from scrapy import signals
  7. class HouseSpiderSpiderMiddleware(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)
  43. class HouseSpiderDownloaderMiddleware(object):
  44. # Not all methods need to be defined. If a method is not defined,
  45. # scrapy acts as if the downloader middleware does not modify the
  46. # passed objects.
  47. @classmethod
  48. def from_crawler(cls, crawler):
  49. # This method is used by Scrapy to create your spiders.
  50. s = cls()
  51. crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
  52. return s
  53. def process_request(self, request, spider):
  54. # Called for each request that goes through the downloader
  55. # middleware.
  56. # Must either:
  57. # - return None: continue processing this request
  58. # - or return a Response object
  59. # - or return a Request object
  60. # - or raise IgnoreRequest: process_exception() methods of
  61. # installed downloader middleware will be called
  62. return None
  63. def process_response(self, request, response, spider):
  64. # Called with the response returned from the downloader.
  65. # Must either;
  66. # - return a Response object
  67. # - return a Request object
  68. # - or raise IgnoreRequest
  69. return response
  70. def process_exception(self, request, exception, spider):
  71. # Called when a download handler or a process_request()
  72. # (from other downloader middleware) raises an exception.
  73. # Must either:
  74. # - return None: continue processing this exception
  75. # - return a Response object: stops process_exception() chain
  76. # - return a Request object: stops process_exception() chain
  77. pass
  78. def spider_opened(self, spider):
  79. spider.logger.info('Spider opened: %s' % spider.name)