middlewares.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import scrapy
  8. from selenium import webdriver
  9. from selenium.webdriver.chrome.options import Options
  10. import time
  11. class AreaSpiderMiddleware(object):
  12. def process_request(self, request, spider):
  13. chrome_options = Options()
  14. chrome_options.add_argument('--headless') # 使用无头谷歌浏览器模式
  15. chrome_options.add_argument('--disable-gpu')
  16. chrome_options.add_argument('--no-sandbox')
  17. # 指定谷歌浏览器路径
  18. self.driver = webdriver.Chrome(chrome_options=chrome_options,executable_path='/root/zx/spider/driver/chromedriver')
  19. if request.url != 'https://www.aqistudy.cn/historydata/':
  20. self.driver.get(request.url)
  21. time.sleep(1)
  22. html = self.driver.page_source
  23. self.driver.quit()
  24. return scrapy.http.HtmlResponse(url=request.url, body=html.encode('utf-8'), encoding='utf-8',
  25. request=request)
  26. class ManhuaSpiderMiddleware(object):
  27. # Not all methods need to be defined. If a method is not defined,
  28. # scrapy acts as if the spider middleware does not modify the
  29. # passed objects.
  30. @classmethod
  31. def from_crawler(cls, crawler):
  32. # This method is used by Scrapy to create your spiders.
  33. s = cls()
  34. crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
  35. return s
  36. def process_spider_input(self, response, spider):
  37. # Called for each response that goes through the spider
  38. # middleware and into the spider.
  39. # Should return None or raise an exception.
  40. return None
  41. def process_spider_output(self, response, result, spider):
  42. # Called with the results returned from the Spider, after
  43. # it has processed the response.
  44. # Must return an iterable of Request, dict or Item objects.
  45. for i in result:
  46. yield i
  47. def process_spider_exception(self, response, exception, spider):
  48. # Called when a spider or process_spider_input() method
  49. # (from other spider middleware) raises an exception.
  50. # Should return either None or an iterable of Response, dict
  51. # or Item objects.
  52. pass
  53. def process_start_requests(self, start_requests, spider):
  54. # Called with the start requests of the spider, and works
  55. # similarly to the process_spider_output() method, except
  56. # that it doesn’t have a response associated.
  57. # Must return only requests (not items).
  58. for r in start_requests:
  59. yield r
  60. def spider_opened(self, spider):
  61. spider.logger.info('Spider opened: %s' % spider.name)
  62. class ManhuaDownloaderMiddleware(object):
  63. # Not all methods need to be defined. If a method is not defined,
  64. # scrapy acts as if the downloader middleware does not modify the
  65. # passed objects.
  66. @classmethod
  67. def from_crawler(cls, crawler):
  68. # This method is used by Scrapy to create your spiders.
  69. s = cls()
  70. crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
  71. return s
  72. def process_request(self, request, spider):
  73. # Called for each request that goes through the downloader
  74. # middleware.
  75. # Must either:
  76. # - return None: continue processing this request
  77. # - or return a Response object
  78. # - or return a Request object
  79. # - or raise IgnoreRequest: process_exception() methods of
  80. # installed downloader middleware will be called
  81. return None
  82. def process_response(self, request, response, spider):
  83. # Called with the response returned from the downloader.
  84. # Must either;
  85. # - return a Response object
  86. # - return a Request object
  87. # - or raise IgnoreRequest
  88. return response
  89. def process_exception(self, request, exception, spider):
  90. # Called when a download handler or a process_request()
  91. # (from other downloader middleware) raises an exception.
  92. # Must either:
  93. # - return None: continue processing this exception
  94. # - return a Response object: stops process_exception() chain
  95. # - return a Request object: stops process_exception() chain
  96. pass
  97. def spider_opened(self, spider):
  98. spider.logger.info('Spider opened: %s' % spider.name)