检测python爬虫时是否代理ip伪装成功的方法
作者:千锋Python唐唐君 时间:2021-07-31 20:23:44
有时候我们的爬虫程序添加了代理,但是我们不知道程序是否获取到了ip,尤其是动态转发模式的,这时候就需要进行检测了,以下是一种代理是否伪装成功的检测方式,这里推介使用亿牛云提供的代码示例。
Python¶
requests
#! -*- encoding:utf-8 -*-
import requests
import random
# 要访问的目标页面
targetUrl = "http://httpbin.org/ip"
# 要访问的目标HTTPS页面
# targetUrl = "https://httpbin.org/ip"
# 代理服务器
proxyHost = "t.16yun.cn"
proxyPort = "31111"
# 代理隧道验证信息
proxyUser = "16ZKBRLB"
proxyPass = "234076"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host" : proxyHost,
"port" : proxyPort,
"user" : proxyUser,
"pass" : proxyPass,
}
# 设置 http和https访问都是用HTTP代理
proxies = {
"http" : proxyMeta,
"https" : proxyMeta,
}
# 设置IP切换头
tunnel = random.randint(1,10000)
headers = {"Proxy-Tunnel": str(tunnel)}
resp = requests.get(targetUrl, proxies=proxies, headers=headers)
print resp.status_code
print resp.text
要检测代理是否获取到,直接在配置代理后访http://httpbin.org/ip网站,获取到ip后再访问www.ip138.com就知道是否获取到了ip。
来源:https://www.cnblogs.com/cherry-tang/p/11064141.html
标签:python,爬虫,代理,ip,伪装
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python进阶之协程详解
2023-08-23 04:52:52
![](https://img.aspxhome.com/file/2023/0/62870_0s.png)
如何捕获人家站点的页面?
2009-11-11 19:19:00
PHP+MYSQL不恶补十句话
2009-12-02 10:09:00
python设置环境变量的作用和实例
2021-07-03 05:03:17
SQL Server数据库服务器高性能设置
2010-11-25 16:00:00
页面中 CSS 加载方式的优化
2008-03-26 12:36:00
发工资啦!教你用Python实现邮箱自动群发工资条
2023-10-12 19:11:17
![](https://img.aspxhome.com/file/2023/1/94141_0s.png)
python plotly画柱状图代码实例
2023-11-29 13:41:31
![](https://img.aspxhome.com/file/2023/2/92392_0s.png)
微信公众平台网页授权获取用户基本信息中授权回调域名设置的变动
2023-11-14 14:01:31
![](https://img.aspxhome.com/file/2023/3/88293_0s.jpg)
利用phpexcel对数据库数据的导入excel(excel筛选)、导出excel
2023-09-04 13:50:42
python 实现图片特效处理
2021-04-20 05:34:22
![](https://img.aspxhome.com/file/2023/5/81595_0s.png)
FrontPage XP设计教程6——制作多媒体网页
2008-10-11 12:38:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017124029644s.jpg)
PyQt5笔记之弹出窗口大全
2021-06-30 06:53:14
![](https://img.aspxhome.com/file/2023/4/99084_0s.jpg)
PHP写的求多项式导数的函数代码
2023-09-10 05:42:56
CSS入门了解CSS的伪类
2007-10-24 19:36:00
Python多线程爬虫实战_爬取糗事百科段子的实例
2021-02-15 16:58:21
Python 基于Selenium实现动态网页信息的爬取
2023-10-31 12:00:22
![](https://img.aspxhome.com/file/2023/3/100773_0s.jpg)
JS中from 表单序列化提交的代码
2023-09-09 22:54:01
解决pycharm启动后总是不停的updating indices...indexing的问题
2023-03-05 07:35:38
Python 照片人物背景替换的实现方法
2021-10-21 08:45:02
![](https://img.aspxhome.com/file/2023/1/100891_0s.png)