爬虫_代理
使用场景
反爬机制:封IP
反反爬策略:使用代理进行请求发送
(破解封IP这种反爬机制)
什么是代理
-
代理服务器
代理的作用
-
突破自身IP访问的限制
-
隐藏自身真实IP
代理相关的网站
-
快代理
-
西祠代理
代理IP的类型
-
http: 应用到http协议对应的URL中
-
https: 应用到https协议对应的URL中
代理IP的匿名度
-
透明:服务器知道该次请求使用了代理,也知道请求对应的真实IP
-
知道使用了代理,不知道真实IP
-
不知道使用了代理,更不知道真实的IP
使用方法:
在post/get请求中在headers后面添加proxies = {"http\https":"对应IP"}
应用
# This Python file uses the following encoding: utf-8 import requests url = 'https://www.sogou.com/sie?query=ip' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55' } response_text = requests.post(url=url, headers=headers, proxies={"http": "202.83.121.90:80"}).text with open('ip.html', 'w', encoding="utf-8") as fp: fp.write(response_text)