deflate与gzip解压的方法几乎相同,源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import zlib import urllib2 def get_url_content(url): headers = { 'Accept-Encoding': 'gzip, deflate', } response = urllib2.urlopen(urllib2.Request(url, headers=headers)) content = response.read() content_encode = response.info().get('Content-Encoding') if content_encode == 'gzip': content = zlib.decompress(content, zlib.MAX_WBITS|16) elif content_encode == 'deflate': content = zlib.decompress(content, -zlib.MAX_WBITS) return content |