python带进度的文件下载函数
admin
2023-07-30 20:14:26
0

1、普通下载

①对于非文本请求,我们可以通过 Response 对象的 content 属性以字节的方式访问请求响应体。

【注意】这种模式只能下载小文件。因为在这种模式下,从服务器接收到的数据是一直储存在内存中,只有当 write 时才写入硬盘,如果文件很大,那么所占用的内存也是很大的。

②下面将一张网络上的图片下载到本地并保存(文件名不变):

import requests

url = 'http://www.hangge.com/blog/images/logo.png'
response = requests.get(url)  # 此时在内存为response响应对象开辟空间,从服务器返回的数据也是一直存储在内存中中
with open("logo.png", "wb") as code:
    code.write(response.content)  # 调用write方法时将内存中的二进制数据写入硬盘

2.流式下载 即边下载边保存。这种方式适合用来下载大文件

import requests

url = 'http://www.hangge.com/blog/images/logo.png'
r = requests.get(url, stream=True)
with open("logo.png", "wb") as f:
    for bl in r.iter_content(chunk_size=1024):
        if bl:
            f.write(bl)

3.带进度的文件下载

如果文件体积很大,下载时我们最好能实时显示当前的下载进度。

为方便使用应该封装一个下载方法(内部同样使用流式下载的方式)

import requests
from contextlib import closing


# 文件下载器
def down_load(file_url, file_path):
    with closing(requests.get(file_url, stream=True)) as response:
        chunk_size = 1024  # 单次请求最大值
        content_size = int(response.headers['content-length'])  # 内容体总大小
        data_count = 0
        with open(file_path, "wb") as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                data_count = data_count + len(data)
                now_jd = (data_count / content_size) * 100
                print("\r 文件下载进度:%d%%(%d/%d) - %s"
                      % (now_jd, data_count, content_size, file_path), end=" ")


if __name__ == '__main__':
    fileUrl = 'http://www.hangge.com/hangge.zip'  # 文件链接
    filePath = "logo.zip"  # 文件路径
    down_load(fileUrl, filePath)

4.带下载速度显示的文件下载

import requests
import time
from contextlib import closing


# 文件下载器
def down_load(file_url, file_path):
    start_time = time.time()  # 文件开始下载时的时间
    with closing(requests.get(file_url, stream=True)) as response:
        chunk_size = 1024  # 单次请求最大值
        content_size = int(response.headers['content-length'])  # 内容体总大小
        data_count = 0
        with open(file_path, "wb") as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                data_count = data_count + len(data)
                now_jd = (data_count / content_size) * 100
                speed = data_count / 1024 / (time.time() - start_time)
                print("\r 文件下载进度:%d%%(%d/%d) 文件下载速度:%dKB/s - %s"
                      % (now_jd, data_count, content_size, speed, file_path), end=" ")


if __name__ == '__main__':
    fileUrl = 'http://www.hangge.com/hangge.zip'  # 文件链接
    filePath = "hangge.zip"  # 文件路径
    down_load(fileUrl, filePath)

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...