增加gzip请求头,让服务器返回gzip数据,通过gzip模块对gzip数据进行解压
import gzip
from urllib import request
url='http://www.sohu.com/'
#指定请求头,让服务器返回gzip数据
headers={
'Accept-Encoding': 'gzip, deflate, br'
}
req=request.Request(url, headers=headers)
with request.urlopen(req) as resp:
print(resp.status, resp.reason,resp.version)
print(resp.headers)
if resp.status==200:
gzipdata = resp.read()
#将服务器发来的gzip数据,解压出来
data = gzip.decompress(gzipdata)
contenttype=resp.getheader('Content-Type')
charset=contenttype.split('charset=')[1]
print( data.decode(charset))