UA池
背景
我们在使用下载中间件处理请求,一般会对请求设置随机的User-Agent,设置随机的代理.目的就是防止爬取网站的反爬虫策略,但是同一类型User-Agent的浏览器还是容易被监测到,开启UA池放置更多类型的User-Agent就能够极大避免反扒机制
作用
- 尽可能多的将scrapy工程中的请求伪装成不同类型的浏览器身份
操作流程
- 在下载中间件中拦截请求
- 将拦截到的请求的请求头信息中的UA进行篡改伪装
- 在settings.py中开启下载中间件
具体代码
#导包 from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware import random #UA池代码的编写(单独给UA池封装一个下载中间件的一个类 ) class RandomUserAgent( UserAgentMiddleware): 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" ] def process_request(self,request,spider): #从列表中随机抽取一个ua值 ua = random.choice(user_agent_list) #ua值进行当前拦截到请求的ua的写入操作 request.headers.setdefault("User-Agent",ua)
ip代理池
作用
- 尽可能多的将scrapy工程中的请求的IP设置成不同的,防止被反扒策略封禁本机IP
操作流程
- 在下载中间件拦截请求
- 将拦截到的请求IP修改成某一代理ip
- 在settings.py中开启下载中间件
具体代码
"""批量对拦截到的请求进行ip更换""" #单独封装下载中间件类 class Proxy(object): #可被选用的代理IP PROXY_http = [ '126.120.102.124:80', '101.208.131.169:56055', ] PROXY_https = [ '20.83.49.90:900', '91.182.12.212:3508', ] def process_exception(self,request,spider): #针对拦截到的url进行判断,(协议头http/https的区分) #request.url 返回值处理为 http/https//:www.xxx.com h = request.url.split(':')[0] #获取请求头协议 if h == "https": ip.random.choice(Proxy_https) request.meta['proxy'] = 'https' + ip else: ip.random.choice(Proxy_http) request.meta['proxy'] = "http" + ip
return request
...