使用请求下载时 tqdm 进度条不显示

tqdm progress bar doesn't show up while downloading with requests

我试图在使用 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)

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

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))