Python progress bar and downloads

前端 未结 10 1178
庸人自扰
庸人自扰 2020-11-28 02:28

I have a python script that launches a URL that is a downloadable file. Is there some way to have python use commandline to display the download progress as oppose to launch

10条回答
  •  忘掉有多难
    2020-11-28 02:40

    There is an answer with requests and tqdm.

    import requests
    from tqdm import tqdm
    
    
    def download(url: str, fname: str):
        resp = requests.get(url, stream=True)
        total = int(resp.headers.get('content-length', 0))
        with open(fname, 'wb') as file, tqdm(
            desc=fname,
            total=total,
            unit='iB',
            unit_scale=True,
            unit_divisor=1024,
        ) as bar:
            for data in resp.iter_content(chunk_size=1024):
                size = file.write(data)
                bar.update(size)
    

    Gist: https://gist.github.com/yanqd0/c13ed29e29432e3cf3e7c38467f42f51

提交回复
热议问题