设置代理

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Tue Apr  7 17:06:49 2020
 4 
 5 @author: ZKYAAA
 6 """
 7 
 8 import urllib.request 
 9 
10 # 查询IP的网址 
11 ip_query_url = "http://ip.chinaz.com/"
12 
13 
14 # 1.创建代理处理器,ProxyHandler参数是一个字典{类型:代理IP:端口} 
15 proxy_support = urllib.request.ProxyHandler({'http': '219.141.153.43:80'}) 
16 
17 # 2.定制,创建一个opener 
18 opener = urllib.request.build_opener(proxy_support) 
19 
20 # 3.安装opener 
21 urllib.request.install_opener(opener) 
22 
23 # 请求头 
24 headers = {     'User-Agent': 'User-Agent:Mozilla/5.0 (X11; Linux x86_64)' 
25                                   ' AppleWebKit/537.36 (KHTML, like Gecko)'  
26                                   ' Chrome/63.0.3239.84 Safari/537.36',     
27                 'Host': 'ip.chinaz.com' 
28 } 
29 req = urllib.request.Request(ip_query_url, headers=headers) 
30 resp = urllib.request.urlopen(req, timeout=60) 
31 html = resp.read().decode('utf-8') 
32 print(html)

 

posted on 2020-04-07 17:28  恒晨  阅读(223)  评论(0)    收藏  举报

导航