如何处理urllib2的缩减响应?

2024-05-17 03:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前使用以下代码通过urllib2解压缩gzip响应:

opener = urllib2.build_opener()
response = opener.open(req)
data = response.read()
if response.headers.get('content-encoding', '') == 'gzip':
    data = StringIO.StringIO(data)
    gzipper = gzip.GzipFile(fileobj=data)
    html = gzipper.read()

它是否也处理压缩响应,或者我需要编写单独的代码来处理压缩响应?


Tags: 代码buildreaddatagetifresponseopen
3条回答

为了回答上述评论,HTTP规范(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3)说:

If no Accept-Encoding field is present in a request, the server MAY assume that the client will accept any content coding. In this case, if "identity" is one of the available content-codings, then the server SHOULD use the "identity" content-coding, unless it has additional information that a different content-coding is meaningful to the client.

我认为这意味着它应该使用身份。我从来没有见过一个服务器没有

你可以试试

if response.headers.get('content-encoding', '') == 'deflate':
    html = zlib.decompress(response.read())

如果失败了,这里有另一种方法,我在requests source code中找到它

if response.headers.get('content-encoding', '') == 'deflate':
    html = zlib.decompressobj(-zlib.MAX_WBITS).decompress(response.read())

有一个更好的方法概述如下:

作者解释了如何逐块解压,而不是在内存中同时解压。当涉及较大的文件时,这是首选方法。

也找到了这个有用的测试站点:

相关问题 更多 >