简体   繁体   中英

How to download a file with Python using tqdm

I want to make a .py file that downloads an image from a link with a progressbar included can I do it with tdqm?

This is what I have so far

from tqdm import tqdm
import requests

chunk_size = 1024

url = "example.com"

r = requests.get(url, stream = True)

total_size = int(r.headers['content-length'])
filename = url.split('/')[-1]

with open(filename, 'wb') as f:
    for data in tqdm(iterable = r.iter_content(chunk_size = chunk_size)):         
        total = total_size/chunk_size, unit = 'KB')
        f.write(data)
print("Download complete!")

Found out how to do it

from tqdm import tqdm
import requests
import math
url = "http://ipv4.download.thinkbroadband.com/5MB"
r = requests.get(url, stream=True)

total_size = int(r.headers.get('content-length', 0))
block_size = 1024
wrote = 0 
with open('output.bin', 'wb') as f:
    for data in tqdm(r.iter_content(block_size):
        total=math.ceil(total_size//block_size) , unit='KB', unit_scale=True)
        wrote = wrote  + len(data)
        f.write(data)
if total_size != 0 and wrote != total_size:
    print("ERROR, something went wrong")  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM