In Python, how do I decode GZIP encoding?

后端 未结 8 1733
星月不相逢
星月不相逢 2020-11-28 07:53

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 sou

8条回答
  •  春和景丽
    2020-11-28 08:43

    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
    

提交回复
热议问题