【发布时间】:2017-12-31 00:21:04
【问题描述】:
对于上下文:我的代码的以下版本可以很好地下载整个图像文件并将其写入磁盘,而无需在写入之前从中读取任何数据。
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(filename, 'wb') as outfile:
for chunk in response.iter_content(chunk_size=256):
outfile.write(chunk)
outfile.close()
我在读取第一个块(包含文件本身的标头——不是 http 响应,不需要它)时的悲惨尝试失败了。
with open(filename, 'wb') as outfile:
chunk1 = response.iter_content(chunk_size=256)
# This gives: '<generator object Response.iter_content.<locals>.generate at 0x033E57E0>'
print(chunk1)
# This fails with error: 'TypeError: a bytes-like object is required, not 'generator'
outfile.write(chunk1)
# Doesn't get to here anymore
for chunk in response.iter_content(chunk_size=256):
outfile.write(chunk)
outfile.close()
我现在很困惑。我不明白为什么chunk1 拒绝被写入,而我的第一个代码版本中for 循环中的所有块都写得很好。是 print(chunk1) 语句以某种方式改变了 chunk1 吗?
我对迭代器的使用不正确吗?
我也不知道如何查看 chunk1 可能具有哪些属性来包含数据...
我也试过
print(response.content)
print(response.raw.data)
# No good: these both download the entire image file, THEN print it to console.
# But they at least print the data itself instead of giving an object
在下载所有内容之前访问标头的目的是,如果标头显示图像因任何原因不受欢迎,则完全停止下载。所以我想我必须以某种方式读取用 iter_contents 检索到的块。
但是我该怎么做呢?
【问题讨论】:
标签: python python-requests response download