请注意,本文编写于 1984 天前,最后修改于 1264 天前,其中某些信息可能已经过时。 ## 下载进度显示 ```python #coding:utf-8 import requests from contextlib import closing #文件下载器 def Down_load(file_url,file_path): headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"} with closing(requests.get(file_url,headers=headers,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__': headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36", } file_url = 'https://down.360safe.com/se/360se8.1.1.404.exe' #文件链接 file_path = "D:\\demo/002.exe" #文件路径 Down_load(file_url,file_path) ``` 最后修改:2021 年 06 月 07 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏