简体   繁体   English

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

[英]How do I make progress bar while downloading file in python

I'm using tqdm to monitor the downloading of files in my python programs but it doesn't show the progress bar.我正在使用 tqdm 来监视我的 python 程序中文件的下载,但它没有显示进度条。 I have this code:我有这个代码:

from tqdm import *
import requests
url = "https://as2.cdn.asset.aparat.com/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]

I tried this code too:我也试过这个代码:

from tqdm import *
import requests
url = "https://as2.cdn.asset.aparat.com/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()

But it gives me the error:但它给了我错误:

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 = "https://as2.cdn.asset.aparat.com/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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中从多个 URL 下载时显示进度条 - How to display progress bar while downloading from multiple URLs in Python 如何制作从大型 xlsx 文件加载 pandas DataFrame 的进度条? - How do I make a progress bar for loading pandas DataFrame from a large xlsx file? 如何在 python 中将文件保存到 excel 时显示进度条? - How to show progress bar while saving file to excel in python? Python - 使用进度条和基本身份验证通过HTTP下载文件 - Python - downloading a file over HTTP with progress bar and basic authentication 完成后如何关闭进度条 - How do I make this progress bar close when it is done 当我们使用 python 从云存储桶下载文件时如何显示进度条 - How to show progress bar when we are downloading a file from cloud bucket using python 如何在Python控制台应用程序中创建不确定的进度栏? - How can I make an indeterminate progress bar in a Python console app? Python 3:如何创建用于下载文件的文本进度栏? - Python 3: How to create a text progress bar for downloading files? 如何在python中的tkinter的消息框中放置进度栏? - How do I put a progress bar in a messagebox in tkinter in python? 如何在不使用Python的情况下在本地下载FTP文件的情况下计算FTP文件中的行数 - How do I count the number of line in a FTP file without downloading it locally while using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM