python: urllib2 using gzip and uncompress
This checks if the content is gzipped and decompresses it:
1 2 3 4 5 6 7 8 9 10 | from StringIO import StringIO import gzip request = urllib2.Request('http://example.com/') request.add_header('Accept-encoding', 'gzip') response = urllib2.urlopen(request) if response.info().get('Content-Encoding') == 'gzip': buf = StringIO( response.read()) f = gzip.GzipFile(fileobj=buf) data = f.read() |
from:http://stackoverflow.com/questions/3947120/does 继续>