Introduction to Urllib (2)———Hanlder Processor

1. Handler processor

Handler
customizes more advanced request headers (customization of complex request objects with business logic can no longer meet our needs (dynamic cookies and proxies cannot use customization of request objects) Examples: Proxy IP (fast proxy), verification code (Super
Eagle )

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)

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/128835653