代理proxies
数据采集过程中使用脚本发送请求,请求次数过于频繁,服务器监测到而采用一定的手段禁止此ip的请求,为了解决封ip的问题,我们用代理来处理这个问题。用第三方代理ip进行伪装访问,即使被封也不影响当前ip的使用,构建代理池,封了一个,其他的还可以用,这样就能缓解ip被封无法继续爬取的问题。
代理匿名度:
(1)透明:目标服务器知道请求使用了代理服务器,同时能监测到真实的请求ip
(2)匿名:目标服务器知道请求使用了代理服务器,但无法监测到真实的请求ip
(3)高匿:目标服务器不知道请求是否使用了代理服务器,也无法监测到真实的请求ip
代理ip类型:(1)http:只能转发http请求;(2)https:只能转发https请求
(1)代理指定
1 import requests 2 from lxml.html.clean import etree 3 from fake_useragent import UserAgent 4 UA=UserAgent() 5 headers={'User-Agent':UA.random,} 6 url='https://www.baidu.com/s?wd=ip' 7 response=requests.get(url=url,headers=headers,proxies={'https':'111.231.94.44:8888'},timeout=10)#代理IP指定 8 response.encoding='utf-8' 9 10 # print(response.text)#在页面中可以搜索‘本机’找到本次请求的代理服务器 11 #<span class="c-gap-right">本机IP: 117.64.251.9</span>安徽省合肥市 电信(代理IP也使用了代理) 12 print(etree.HTML(response.text).xpath('//span[@class="c-gap-right"]/text()')[0])