网页压缩技术主要两种gzip和deflate
DEFLATE是一个无专利的压缩算法,它可以实现无损数据压缩,有众多开源的实现算法。
GZIP是使用DEFLATE进行压缩数据的另一个压缩库。
现在普遍支持gzip压缩,Deflate只是一种过时的网页压缩
if __name__ == "__main__":
url='http://www.qq.com/'
req = request.Request(url)
response = request.urlopen(req, timeout=120)
html = response.read()
encoding = response.info().get('Content-Encoding')
print(encoding)
if encoding == 'gzip':
html = zlib.decompress(html, 16+zlib.MAX_WBITS)
elif encoding == 'deflate':
try:
html = zlib.decompress(html, -zlib.MAX_WBITS)
except zlib.error:
html = zlib.decompress(html)
charset = chardet.detect(html)["encoding"]
print(charset)
#print(html)
print(html.decode(charset,'ignore'))