# 一些概念
- 在scrapy中如何给所有的请求对象尽可能多的设置不一样的请求载体身份标识 - UA池,process_request(request) - 在scrapy中如何给发生异常的请求设置代理ip - ip池,process_exception(request,response,spider):request.meta['proxy'] = 'http://ip:port' - 将异常的请求拦截到之后,通过代理ip相关的操作,就可以将改异常的请求变成非异常的请求,然后必须重新对该请求进行重新的请求发送:return request - 简述下载中间件的作用及其最重要三个方法的作用 - process_request:拦截所有非异常的请求 (正常的请求) - process_response:拦截所有的响应对象 - process_exception:拦截发生异常的请求对象,需要对异常的请求对象进行相关处理,让其变成正常的请求对象,然后通过return request 对该请求进行重新发送 - 简述scrapy中什么时候需要使用selenium及其scrapy应用selenium的编码流程 - 实例化浏览器对象(一次):spider的init方法 或者全局属性 - 需要编写浏览器自动化的操作(中间件的process_response) - 关闭浏览器(spider的closed方法中进行关闭操作)
self.bro.quit()
# UA池 代理IP池 scrapy的下载中间件
# -*- coding: utf-8 -*- from scrapy import signals import random class MiddleproDownloaderMiddleware(object): user_agent_list = [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 " "(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 " "(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 " "(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 " "(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 " "(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 " "(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 " "(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 " "(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 " "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 " "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24" ] PROXY_http = [ '153.180.102.104:80', '195.208.131.189:56055', ] PROXY_https = [ '120.83.49.90:9000', '95.189.112.214:35508', ] @classmethod def from_crawler(cls, crawler): s = cls() crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) return s #可以处理拦截到所有的非异常的请求 #spider参数表示的就是爬虫类实例化的一个对象 def process_request(self, request, spider): print('this is process_request()') #UA伪装 request.headers['User-Agent'] = random.choice(self.user_agent_list) #测试:代理操作是否生效 request.meta['proxy'] = 'https://218.60.8.83:3129' return None #拦截所有的响应 def process_response(self, request, response, spider): return response #拦截发生异常的请求对象 def process_exception(self, request, exception, spider): if request.url.split(':')[0] == 'https': request.meta['proxy'] = 'https://'+random.choice(self.PROXY_https) else: request.meta['proxy'] = 'http://' + random.choice(self.PROXY_http) return request def spider_opened(self, spider): spider.logger.info('Spider opened: %s' % spider.name)
- 在scrapy中如何给所有的请求对象尽可能多的设置不一样的请求载体身份标识 - UA池,process_request(request) - 在scrapy中如何给发生异常的请求设置代理ip - ip池,process_exception(request,response,spider):request.meta['proxy'] = 'http://ip:port' - 将异常的请求拦截到之后,通过代理ip相关的操作,就可以将改异常的请求变成非异常的请求,然后必须重新 对该请求进行重新的请求发送:return request - 简述下载中间件的作用及其最重要三个方法的作用 - process_request:拦截所有非异常的请求 - process_response:拦截所有的响应对象 - process_exception:拦截发生异常的请求对象,需要对异常的请求对象进行相关处理,让其变成 正常的请求对象,然后通过return request 对该请求进行重新发送 - 简述scrapy中什么时候需要使用selenium及其scrapy应用selenium的编码流程 - 实例化浏览器对象(一次):spider的init方法 - 需要编写浏览器自动化的操作(中间件的process_response) - 关闭浏览器(spider的closed方法中进行关闭操作)