代理
破解封IP这种反爬机制。访问次数太多,访问过密过快,IP会被封。
概念
什么是代理?
- 代理服务器。
代理的作用?
- 突破自身IP访问的限制。
- 隐藏自身真实的IP
代理相干网站:
- 快代理
- 西祠代理
- www.goubanjia.com
代理ip的类型:
- https:应用到https对应的url中
- http:应用到http对应的url中
代理ip的匿名度:
- 透明:服务器知道该次请求使用了代理,也知道请求对应的真实IP
- 匿名:服务器知道该次请求使用了代理,但不知道请求对应的真实IP
- 高匿:服务器不知道该次请求使用了代理,也不知道请求对应的真实IP
在爬虫中的应用
import requests
url = 'https://www.baidu.com/s?wd=ip' # 百度查询ip地址url
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.50'
}
page_text = requests.get(url,headers=headers,proxies={'https':'222.249.238.138:8080'}).text
# 将代理ip封装到get或post请求中,格式为proxies:{'ip类型':'ip地址及端口号'}
with open ('ip.html','w',encoding='utf-8') as fp:
fp.write(page_text)