在Python中,如何解码GZIP编码? [英] In Python, how do I decode GZIP encoding?

查看:1634
本文介绍了在Python中,如何解码GZIP编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的python脚本中下载了一个网页。
在大多数情况下,这样做很好。



然而,这个人有一个响应标题:GZIP编码,当我尝试打印这个



如何将其解码为常规文本?

解决方案

我使用zlib从网页解压缩gziped的内容。

  import zlib 

...
#f = urllib2.urlopen(url)
decompressed_data = zlib.decompress(f.read(),16 + zlib.MAX_WBITS)


I downloaded a webpage in my python script. In most cases, this works fine.

However, this one had a response header: GZIP encoding, and when I tried to print the source code of this web page, it had all symbols in my putty.

How do decode this to regular text?

解决方案

I use zlib to decompress gzipped content from web.

import zlib

...
# f=urllib2.urlopen(url) 
decompressed_data=zlib.decompress(f.read(), 16+zlib.MAX_WBITS)

这篇关于在Python中,如何解码GZIP编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆