In Python, how do I decode GZIP encoding?

TIMEX picture TIMEX · Apr 23, 2010 · Viewed 57k times · Source

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?

Answer

YOU picture YOU · Apr 23, 2010

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)