Search code examples
pythonhttptextencodingascii

In Python, how do I decode GZIP encoding?


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?


Solution

  • I use zlib to decompress gzipped content from web.

    import zlib
    import urllib
    
    f=urllib.request.urlopen(url) 
    decompressed_data=zlib.decompress(f.read(), 16+zlib.MAX_WBITS)