zkwgpp 2020-05-07
#coding=utf-8 #HTTPHandler & opener #更高级一些功能,可以高仿模拟浏览器 import urllib.request import urllib.parse url = "https://www.baidu.com/"; handler = urllib.request.HTTPHandler(); opener = urllib.request.build_opener(handler); headers={ ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘ }; request = urllib.request.Request(url =url,headers=headers); response = opener.open(request); print(response.read());
1.什么是HTTPHandler
HttpHandler是一个HTTP请求的真正处理中心,也正是在这个HttpHandler容器中,python才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中