随请求下载时,tqdm进度栏不显示

时间:2019-06-23 19:46:44

标签: python python-3.x python-requests tqdm

我正在尝试在使用python请求库下载文件时显示tqdm的进度条。但是它没有显示进度条。它显示了这一点

22964708: 2804it [00:13, 204.17it/s]

这是我的代码:

with requests.get(url, stream=True) as r:
    r.raise_for_status()
    with open("downloads/" + name, 'wb') as f:
        for chunk in tqdm(r.iter_content(chunk_size=8192),r.headers.get("content-length")):
            if chunk:
                f.write(chunk)

1 个答案:

答案 0 :(得分:0)

在下载时显示进度条的代码是

from tqdm import *
import requests
url = "your url"
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))