python urllib2是否会自动解压缩从网页获取的gzip数据?

python urllib2是否会自动解压缩从网页获取的gzip数据?,第1张

python urllib2是否会自动解压缩从网页获取的gzip数据
  1. 如何判断URL上的数据是否已压缩?

这将检查内容是否被压缩并解压缩:

from StringIO import StringIOimport gziprequest = 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()
  1. 如果将urllib2压缩,数据是否会自动解压缩?数据将始终是字符串吗?

否。urllib2不会自动解压缩数据,因为urllib2并未设置’Accept-Encoding’标头,而是由您使用:

request.add_header('Accept-Encoding','gzip, deflate')



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655067.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存