This article mainly introduced about Python use proxy IP access to the site, has a certain reference value, now share to everyone, have the need for friends can refer to
The examples are as follows:
#-*-Coding:utf-8-*-from urllib import requestif __name__ = = "__main__": #访问网址 url = ' http://www.whatismyip.com . tw/' #这是代理IP ip = ' 27.155.101.233:3128 ' #设置代理ip访问方式, HTTP and https proxy = {' http ': IP, ' https ': IP} #创建ProxyHandler proxy_support = Request. Proxyhandler (proxy) #创建Opener opener = Request.build_opener (proxy_support) #添加User angent Opener.addheaders = [(' User-agent ', ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.221 safari/537.36 SE 2.X METASR 1.0 ')] #安装OPener
request.install_opener (opener) #使用自己安装好的Opener response = request.urlopen (URL) #读取相应信息并解码 html = Response.read (). Decode ("Utf-8") #打印信息 print (HTML)
Whatismyip is a query IP site, the above code I used 27.155.101.233, the port number is 3128 access to this site
Here is the output information:
Can see already used 27.155.101.233 this proxy IP to Whatismyip access, but did not do high stealth, by this site found the real IP.
But there's no problem in dealing with most polls.