Python 下载大文件,哪种方式速度更快

通常,我们都会用 requests 库去下载,这个库用起来太方便了。

方法一

使用以下流式代码,无论下载文件的大小如何,Python 内存占用都不会增加:

def download_file(url):
    local_filename = url.split('/')[-1]
    # 注意传入参数 stream=True
    with requests.get(url, stream=Trueas r:
        r.raise_for_status()
        with open(local_filename, 'wb'as f:
            for chunk in r.iter_content(chunk_size=8192): 
                f.write(chunk)
    return local_filename

如果你有对 chunk 编码的需求,那就不该传入 chunk_size 参数,且应该有 if 判断。

def download_file(url):
    local_filename = url.split('/')[-1]
    # 注意传入参数 stream=True
    with requests.get(url, stream=Trueas r:
        r.raise_for_status()
        with open(local_filename, 'w'as f:
            for chunk in r.iter_content(): 
                if chunk:
                    f.write(chunk.decode("utf-8"))
    return local_filename

iter_content[1] 函数本身也可以解码,只需要传入参数 decode_unicode = True 即可。

请注意,使用 iter_content 返回的字节数并不完全是 chunk_size,它是一个通常更大的随机数,并且预计在每次迭代中都会有所不同。

方法二

使用 Response.raw[2]shutil.copyfileobj[3]

import requests
import shutil

def download_file(url):
    local_filename = url.split('/')[-1]
    with requests.get(url, stream=Trueas r:
        with open(local_filename, 'wb'as f:
            shutil.copyfileobj(r.raw, f)

    return local_filename

这将文件流式传输到磁盘而不使用过多的内存,并且代码更简单。

注意:根据文档,Response.raw 不会解码,因此如果需要可以手动替换 r.raw.read 方法

response.raw.read = functools.partial(response.raw.read, decode_content=True)

速度

方法二更快。方法一如果 2-3 MB/s 的话,方法二可以达到近 40 MB/s。

最后

如果用 Python 更快的下载大文件,推荐使用方法二。如果有收获,还请点赞、转发,关注。

参考资料

[1]

iter_content: https://requests.readthedocs.io/en/latest/api/#requests.Response.iter_content

[2]

Response.raw: https://requests.readthedocs.io/en/latest/api/#requests.Response.raw

[3]

shutil.copyfileobj: https://docs.python.org/3/library/shutil.html#shutil.copyfileobj


原文始发于微信公众号(Python七号):Python 下载大文件,哪种方式速度更快

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/39672.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!