python之urllib.request处理gzip数据 作者:马育民 • 2019-01-15 10:17 • 阅读:10116 增加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)) ``` 原文出处:http://www.malaoshi.top/show_1EF2cApyDUbY.html