#IP代理池的第二种方式,接口调用法(适用于不稳定的IP)
import urllib.request
def ip():
thisip=urllib.request.urlopen("http://tvp.daxiangdaili.com/ip?tid=559126871522487&num=1&foreign=only").read().decode("utf-8","ignore")
print(thisip)
proxy=urllib.request.ProxyHandler({"http":thisip})
opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
for i in range(0,3):
try:#因为会哦失效所以要检查异常
ip()
url="http://www.baidu.com"
data=urllib.request.urlopen(url).read()
#.decode("utf-8","ignore")
print(len(data))
fh=open("D:\\pythonprojects\\result\\"+str(i)+".html",'wb')
#.decode("utf-8","ignore")
print(len(data))
fh.write(data)
fh.close()
except Exception as err:
print(err)
IP代理池的第二种方式,接口调用法(适用于不稳定的IP)
最新推荐文章于 2024-05-27 14:17:02 发布