通过上次写的自动获取IP代理池,访问whatismyip,来测试ip代理访问

来源:互联网 发布:吴天心知乎 编辑:程序博客网 时间:2024/05/22 06:51

代码有时运行不成功,可能是代理地址问题。文件简单介绍创建opener对象。

优秀文章:http://www.jb51.net/article/46495.htm

#-*-coding:UTF-8-*-#author:若鸟#date:2017-1-3#function:通过获得的代理IP,实现IP代理登录,查看自己IP测试import urllib.requestimport randomfrom ProxyIP import GetProxyIPurl = "http://whatismyip.org/"      ipList=GetProxyIP._get_Proxy_IP()                                               #获取自己爬取的代理池proxyIpSupport = urllib.request.ProxyHandler({"http":random.choice(ipList)})                #实现IP代理     opener = urllib.request.build_opener(proxyIpSupport)                            #创建一个opener对象。urllib.request.install_opener(opener)                                           #安装openeropener.addheaders= [("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0")]     #添加头部seq = urllib.request.Request(url)req = urllib.request.urlopen(seq)html = req.read().decode("utf-8")print(

0 0
原创粉丝点击