Search code examples
pythonpython-3.xpython-requeststqdm

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


I'm trying to show a progress bar with tqdm while downloading a file with python requests library. But it doesn't show the progress bar. It shows this

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

And this is my code:

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)

Solution

  • It's the code that shows progress bar while downloading:

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