在python中下载文件时如何制作进度栏

编程入门 行业动态 更新时间:2024-10-24 08:32:39
本文介绍了在python中下载文件时如何制作进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用tqdm监视python程序中文件的下载,但未显示进度栏. 我有以下代码:

I'm using tqdm to monitor the downloading of files in my python programs but it doesn't show the progress bar. I have this code:

from tqdm import * import requests url = "as2.cdn.asset.aparat/aparat-video/520055aa72618571e4ce34b434e328b615570838-144p__58945.mp4" name = "video" with requests.get(url, stream=True) as r: r.raise_for_status() with open(name, 'wb') as f: for chunk in tqdm(r.iter_content(chunk_size=8192), r.headers.get("content-length")): if chunk: # filter out keep-alive new chunks f.write(chunk) # f.flush()

但是当我运行它时,它没有显示进度条,而是向我显示了此内容:

But when I run it, it doesn't show me a progress bar, it shows me this:

763499: 94it [00:00, 192.31it/s]

我也尝试过此代码:

from tqdm import * import requests url = "as2.cdn.asset.aparat/aparat-video/520055aa72618571e4ce34b434e328b615570838-144p__58945.mp4" name = "asdasdjk" with requests.get(url, stream=True) as r: r.raise_for_status() with open(name, 'wb') as f: for chunk, bar in r.iter_content(chunk_size=8192), r.headers.get("content-length"),tqdm(range(0,int(r.headers.get("content-length")))): if chunk: # filter out keep-alive new chunks f.write(chunk) # f.flush()

但是它给了我错误:

Exception has occurred: ValueError too many values to unpack (expected 2) File "test.py", line 8, in <module> for chunk, bar in r.iter_content(chunk_size=8192), r.headers.get("content-length"),tqdm(range(0,int(r.headers.get("content-length")))):

推荐答案

from tqdm import * import requests url = "as2.cdn.asset.aparat/aparat-video/520055aa72618571e4ce34b434e328b615570838-144p__58945.mp4" name = "video" with requests.get(url, stream=True) as r: r.raise_for_status() with open(name, 'wb') as f: pbar = tqdm(total=int(r.headers['Content-Length'])) for chunk in r.iter_content(chunk_size=8192): if chunk: # filter out keep-alive new chunks f.write(chunk) pbar.update(len(chunk))

更多推荐

在python中下载文件时如何制作进度栏

本文发布于:2023-11-26 07:07:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   文件   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!