使用build_opener 自定义 opener,这种方法的好处是可以方便的拓展功能。
import urllib.request
import http.cookiejar
def makeMyOpener(head={
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'}):
cj = http.cookiejar.CookieJar()
cj_process = urllib.request.HTTPCookieProcessor()
# 定义opener
opener = urllib.request.build_opener(cj_process)
header = []
for key,value in head.items():
header.append((key,value))
# opener变量add_headers为一个list
opener.addheaders = header
return opener
oper = makeMyOpener()#opener对象
uop = oper.open('http://www.baidu.com/',timeout=1000)
data = uop.read().decode('utf-8')
print(data)
以下是利用Fiddler抓到的GET报文