对于上下文:我的代码的以下版本可以正常下载+将整个映像文件写入磁盘,而无需在写入之前从中读取任何数据。
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 检索的块。
但我该怎么做?
答案 0 :(得分:3)
你混淆的是使用发电机。你无法保存chunk1
,你想使用next
从发电机获取下一件作品:
outfile.write(next(chunk1))
import requests
url = 'https://raw.githubusercontent.com/mattupstate/flask-mail/master/flask_mail.py'
filename = 'flask_mail.py'
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(filename, 'wb') as outfile:
# get the next chunk and save to disk
outfile.write(next(response.iter_content(chunk_size=256)))
for chunk in response.iter_content(chunk_size=256):
outfile.write(chunk)
请注意,当您使用上下文管理器(close
)时,您不需要with open(...
。