4
我一直在嘗試在python 3.6中使用Tqdm模塊設置進度條,但似乎我在這裏一半。使用Tqdm在下載文件時添加進度條
我的代碼如下:
from tqdm import tqdm
import requests
import time
url = 'http://alpha.chem.umb.edu/chemistry/ch115/Mridula/CHEM%20116/documents/chapter_20au.pdf'
# Streaming, so we can iterate over the response.
r = requests.get(url, stream=True)
#Using The Url as a filename
local_filename = url.split('/')[-1]
# Total size in bytes.
total_size = int(r.headers.get('content-length', 0))
#Printing Total size in Bytes
print(total_size)
#TQDM
with open(local_filename, 'wb') as f:
for data in tqdm(r.iter_content(chunk_size = 512), total=total_size, unit='B', unit_scale=True):
f.write(data)
的問題是,當我插入chunk_size = 512
在r.iter_content
同時顯示下載數據的進度條不加載在所有的,但是當我完全刪除chunk_size = 512
和將圓括號留空即可,因爲下載速度非常糟糕。
我在這裏做錯了什麼?