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
和将圆括号留空即可,因为下载速度非常糟糕。
我在这里做错了什么?