【问题标题】:Reading data from a file during download with Requests.response在下载期间使用 Requests.response 从文件中读取数据
【发布时间】: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


    【解决方案1】:

    您的困惑在于使用生成器。您无法保存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)
    

    请注意,当您是上下文管理器 (with open(...) 时,您不需要 close

    【讨论】:

    • 谢谢,现在要审查迭代器...!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多