文章目录
1. 代理ip设置1.0 透明、普匿、高匿ip区别1.1 代理设置格式1.2 详细测试1.3 报错4072. 验证ip是否可用demo(遇到不可用ip程序会停止)2.1 验证网站2.2 代码及结果2.2.1 /get2.2.2 /3. 验证ip是否可用完整代码(使用try-except)3.1 代码1. 代理ip设置
1.0 透明、普匿、高匿ip区别
(1) 透明代理
代理服务器将客户端的信息转发至目标访问对象,并没有完全隐藏客户端真实的身份。即服务器知道客户端使用了代理IP,并且知道客户端的真实IP地址。
(2) 普通匿名代理
代理服务器用自己的IP代替了客户端的真实IP,但是告诉了目标访问对象这是代理访问。
(3) 高匿代理
代理服务器良好地伪装了客户端,不但用一个随机的IP代替了客户端的IP,也隐藏了代理信息,服务器不会察觉到客户端是通过代理实现访问的,即用户仿佛就是直接使用代理服务器作为自己的客户端,618国内IP代理就是高匿名的服务器。
所以使用高匿代理就可以隐藏自己的真实IP了
1.1 代理设置格式
1.proxies在你访问http时用http的设置,访问https时用https的设置,所以你的proxy需要同时包含http及https的配置,这样才能生效。
2.如果访问http网站时,可你的proxies={“https”:"*****"}是这样,即只写了https的,那么请求时候会自动使用本机ip。
3.同一个ip(不论它是http、还是https)在proxies里,即可用于访问http,也可访问https的,只需要按下方代码设置即可。
因此格式如下:
import requestsproxies = {'http': 'http://222.89.32.159:21079','https': 'http://222.89.32.159:21079'}headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}res = requests.get(url=urls,headers=headers,proxies=proxies)
1.2 详细测试
若要了解详情,这里有人进行过详细测试:
详细测试
1.3 报错407
提示
找了很久,说是要代理身份 验证,这个问题搞得我很晕!
后来才想起来,我用的是网上购买的代理ip。想要使用购买的代理ip要不就是把本地ip添加到白名单,要不就是使用用户名+密码进行验证。后来发现本地ip又更新了,不在白名单里,因此再次把本地ip添加到白名单后,上述代码就没问题了!!!
2. 验证ip是否可用demo(遇到不可用ip程序会停止)
这一步主要是创建自己的代理ip池:爬取网上免费ip,然后验证是否可用,可用自己保存起来。
2.1 验证网站
这里介绍两个验证网站:
1.验证以http开头ip的:
这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。
它用 Python + Flask 编写,是一个开源项目。
网站
git
使用方法
2. 验证以https开头的ip:/
也会返回代理ip地址
2.2 代码及结果
2.2.1 /get
import requestsurls="/"url = "/get"proxies = {'http': 'http://113.123.45.140:18928','https': 'http://125.87.99.138:15759'}headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}res = requests.get(url=url,headers=headers,proxies=proxies)#发起请求if res.status_code == 200:print(res.status_code) print(res.text)else:print(res.status_code)#print(res.text)
结果:
2.2.2 /
代码:
import requestsurls="/"url = "/get"proxies = {'http': 'http://113.123.45.140:18928','https': 'http://125.87.99.138:15759'}headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}res = requests.get(url=urls,headers=headers,proxies=proxies)#发起请求if res.status_code == 200:print(res.status_code) print(res.text)else:print(res.status_code)#print(res.text)
结果:
3. 验证ip是否可用完整代码(使用try-except)
3.1 代码
import requestsurls="/"url = "/get"proxies0 = {'http': 'http://123.171.1.127:9','https': 'http://123.171.1.127:9'}proxies1 = {'http': 'http://180.121.130.41:22485','https': 'http://180.121.130.41:22485'}proxies2 = {'http': 'http://163.204.241.181:9999','https': 'http://163.204.241.181:9999'}proxies3 = {'http': 'http://180.105.100.117:23075','https': 'http://180.105.100.117:23075'}headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}proxies=[proxies0,proxies1,proxies2,proxies3]for pro in proxies:try:# timeout=5 是相应超过5s就放弃本次请求执行下一次res = requests.get(url=urls, headers=headers, proxies=pro,timeout=5)print(pro["https"],"可用,",res.status_code)except:print(pro["https"],"不可用,")
结果: