在使用Requests.response下载期间从文件中读取数据

问题描述 投票:2回答:1

对于上下文:以下版本的代码可以正常下载+将整个映像文件写入磁盘,而无需在写入之前从中读取任何数据。

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 downloading
1个回答
3
投票

你混淆的是使用发电机。你不能保存chunk1,你想使用next从发电机获得下一件作品:

Code:

outfile.write(next(chunk1))

Full Code:

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(...

© www.soinside.com 2019 - 2024. All rights reserved.