From adfd1e76934c071d4f14f5c2cd00fc61e14b0438 Mon Sep 17 00:00:00 2001 From: JLUVicent <17390955615@163.com> Date: Tue, 14 Sep 2021 22:01:15 +0800 Subject: [PATCH] =?UTF-8?q?.\16=5Furllib=5F=E4=BB=A3=E7=90=86=E6=B1=A0.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...b_\344\273\243\347\220\206\346\261\240.py" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "16_urllib_\344\273\243\347\220\206\346\261\240.py" diff --git "a/16_urllib_\344\273\243\347\220\206\346\261\240.py" "b/16_urllib_\344\273\243\347\220\206\346\261\240.py" new file mode 100644 index 0000000..a61d598 --- /dev/null +++ "b/16_urllib_\344\273\243\347\220\206\346\261\240.py" @@ -0,0 +1,34 @@ +# 随机调用代理池中的Ip进行访问 + +import random +import urllib.request + +url = 'http://www.baidu.com/s?wd=ip' + +# 代理ip不稳定,如需要代理ip则要购买,这里展示代理池的用法。 +proxis_pool = [ + {'http': '211.65.197.93:80'}, + {'http': '211.65.197.93:80'} +] + + +proxis = random.choice(proxis_pool) + +headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36' +} + +# 请求对象的定制 +request = urllib.request.Request(url=url, headers=headers) + +# print(proxis) + +# 用代理必须用下面三个获取响应 +handler = urllib.request.ProxyHandler(proxies=proxis) +opener = urllib.request.build_opener(handler) +response = opener.open(request) + +content = response.read().decode('utf-8') + +with open('dailipool.html', 'w', encoding='utf-8')as fp: + fp.write(content)