urllib.request.ProxyHandler()
可以动态设置代理IP池
urllib.request.build_opener()
将代理IP池中的ip写入该方法生成opener
opener.open(url)
打开url
代码如下:
# 导入urllib import urllib.request # 设置url url = 'https://movie.douban.com/' # 设置代理IP池 proxy_handler = urllib.request.ProxyHandler({ 'http':'218.56.132.157:8080', 'https':'183.30.197.29:9797' }) # 将代理ip池中ip生成对象 opener = urllib.request.build_opener(proxy_handler) html = opener.open(url).read().decode('utf-8') f = open('html.txt','w',encoding='utf-8') f.write(html) f.close()