python gzip压缩

 1 import urllib2
 2 from StringIO import StringIO
 3 import gzip
 4  
 5 def loadData(url):
 6     request = urllib2.Request(url)
 7     request.add_header('Accept-encoding', 'gzip')
 8     response = urllib2.urlopen(request)
 9     if response.info().get('Content-Encoding') == 'gzip':
10         print 'gzip enabled'
11         buf = StringIO(response.read())
12         f = gzip.GzipFile(fileobj=buf)
13         data = f.read()
14     else:
15         data = response.read()
16     return data
posted @ 2017-01-21 11:32  菲菲菲菲菲常新的新手  阅读(664)  评论(0编辑  收藏  举报