簡體   English   中英

如何使用 tqdm 使用 Python 下載文件

[英]How to download a file with Python using tqdm

我想制作一個從包含進度條的鏈接下載圖像的 .py 文件,我可以用 tdqm 來做嗎?

這是我到目前為止

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

了解如何去做

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM