为了账号安全,请及时绑定邮箱和手机立即绑定

python爬虫时如何知道是否代理ip伪装成功

标签:
Python 大数据


      有时候我们的爬虫程序添加了代理,但是我们不知道程序是否获取到了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。


点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消