Urllib的介绍(2)————Hanlder处理器

1.Handler处理器

Handler
定制更高级的请求头(随着业务逻辑的复杂 请求对象的定制已经满足不了我们的需求(动态cookie和代理 不能使用请求对象的定制)
例子:代理IP(快代理),验证码(超级鹰)

import urllib.request 
url = 'http://www.baidu.com/s?wd=ip' 
headers = {
    
     'User ‐ Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3729.169Safari / 537.36' } 
request = urllib.request.Request(url=url,headers=headers) 
proxies = {
    
    'http':'117.141.155.244:53281'} 
handler = urllib.request.ProxyHandler(proxies=proxies) 
opener = urllib.request.build_opener(handler) 
response = opener.open(request) 
content = response.read().decode('utf‐8') with open('daili.html','w',encoding='utf‐8')as fp: 
	fp.write(content)

猜你喜欢

转载自blog.csdn.net/guoguozgw/article/details/128835653