TIMEX
TIMEX

Reputation: 271744

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?

Upvotes: 53

Views: 96219

Answers (9)

kmoser
kmoser

Reputation: 9273

None of these answers worked out of the box using Python 3. Here is what worked for me to fetch a page and decode the gzipped response:

import requests
import gzip

response = requests.get('your-url-here')
data = str(gzip.decompress(response.content), 'utf-8')
print(data)  # decoded contents of page

Upvotes: 4

YOU
YOU

Reputation: 123831

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)

Upvotes: 109

Gerard G
Gerard G

Reputation: 301

This version is simple and avoids reading the whole file first by not calling the read() method. It provides a file stream like object instead that behaves just like a normal file stream.

import gzip
from urllib.request import urlopen

my_gzip_url = 'http://my_url.gz'
my_gzip_stream = urlopen(my_gzip_url)
my_stream = gzip.open(my_gzip_stream, 'r')

Upvotes: 2

John Machin
John Machin

Reputation: 82934

Decompress your byte stream using the built-in gzip module.

If you have any problems, do show the exact minimal code that you used, the exact error message and traceback, together with the result of print repr(your_byte_stream[:100])

Further information

1. For an explanation of the gzip/zlib/deflate confusion, read the "Other uses" section of this Wikipedia article.

2. It can be easier to use the zlib module than the gzip module if you have a string rather than a file. Unfortunately the Python docs are incomplete/wrong:

zlib.decompress(string[, wbits[, bufsize]])

...The absolute value of wbits is the base two logarithm of the size of the history buffer (the “window size”) used when compressing data. Its absolute value should be between 8 and 15 for the most recent versions of the zlib library, larger values resulting in better compression at the expense of greater memory usage. The default value is 15. When wbits is negative, the standard gzip header is suppressed; this is an undocumented feature of the zlib library, used for compatibility with unzip‘s compression file format.

Firstly, 8 <= log2_window_size <= 15, with the meaning given above. Then what should be a separate arg is kludged on top:

arg == log2_window_size means assume string is in zlib format (RFC 1950; what the HTTP 1.1 RFC 2616 confusingly calls "deflate").

arg == -log2_window_size means assume string is in deflate format (RFC 1951; what people who didn't read the HTTP 1.1 RFC carefully actually implemented)

arg == 16 + log_2_window_size means assume string is in gzip format (RFC 1952). So you can use 31.

The above information is documented in the zlib C library manual ... Ctrl-F search for windowBits.

Upvotes: 36

simhumileco
simhumileco

Reputation: 34535

If you use the Requests module, then you don't need to use any other modules because the gzip and deflate transfer-encodings are automatically decoded for you.

Example:

>>> import requests
>>> custom_header = {'Accept-Encoding': 'gzip'}
>>> response = requests.get('https://api.github.com/events', headers=custom_header)
>>> response.headers
{'Content-Encoding': 'gzip',...}
>>> response.text
'[{"id":"9134429130","type":"IssuesEvent","actor":{"id":3287933,...

The .text property of the response is for reading the content in the text context.

The .content property of the response is for reading the content in the binary context.

See the Binary Response Content section on docs.python-requests.org

Upvotes: 9

Shatu
Shatu

Reputation: 1839

For Python 3

Try out this:

import gzip

fetch = opener.open(request) # basically get a response object
data = gzip.decompress(fetch.read())
data = str(data,'utf-8')

Upvotes: 19

whitebeard
whitebeard

Reputation: 1131

Similar to Shatu's answer for python3, but arranged a little differently:

import gzip

s = Request("https://someplace.com", None, headers)
r = urlopen(s, None, 180).read()
try: r = gzip.decompress(r)
except OSError: pass
result = json_load(r.decode())

This method allows for wrapping the gzip.decompress() in a try/except to capture and pass the OSError that results in situations where you may get mixed compressed and uncompressed data. Some small strings actually get bigger if they are encoded, so the plain data is sent instead.

Upvotes: 3

Druska
Druska

Reputation: 4941

You can use urllib3 to easily decode gzip.

urllib3.response.decode_gzip(response.data)

Upvotes: -1

Michał Niklas
Michał Niklas

Reputation: 54302

I use something like that:

f = urllib2.urlopen(request)
data = f.read()
try:
    from cStringIO import StringIO
    from gzip import GzipFile
    data2 = GzipFile('', 'r', 0, StringIO(data)).read()
    data = data2
except:
    #print "decompress error %s" % err
    pass
return data

Upvotes: 11

Related Questions