【发布时间】:2014-09-09 16:07:58
【问题描述】:
我正在使用requests下载文件:
import requests
req = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
gzip 文件的问题是它们被请求自动解码,因此我在磁盘上获取解压文件,而我需要原始文件。
有没有办法告诉请求不要这样做?
【问题讨论】:
-
这是我在谷歌搜索“python requests gzip”时发现的:"Requests automatically decompresses gzip-encoded responses ... You can get direct access to the raw response (and even the socket), if needed as well." 然后我在文档中搜索原始响应并找到
requests.Response.raw;也许这就是你需要的? -
您正确显示的代码为我下载了
.gz文件。你用的是什么服务器?req.headers的值是多少?您下载的 URL 是否公开可用,您可以与我们分享吗? -
只是为了确定,您能告诉我们您是如何确定要在磁盘上获取解压文件的吗?
-
你能用
url='https://wiki.mozilla.org/images/f/ff/Example.json.gz'和local_filename='Example.json.gz'试试你的代码吗?那还会自动解压吗?