代碼有時運行不成功,可能是代理地址問題。文件簡單介紹創建opener對象。
優秀文章:http://www.jb51.net/article/46495.htm
#-*-coding:UTF-8-*- #author:若鳥 #date:2017-1-3 #function:通過獲得的代理IP,實現IP代理登錄,查看自己IP測試 import urllib.request import random from ProxyIP import GetProxyIP url = "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) #安裝opener opener.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(