小编use*_*335的帖子

使用Tqdm在下载文件时添加进度条

我一直在尝试使用 python 3.6 中的 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)
Run Code Online (Sandbox Code Playgroud)

问题是,当我chunk_size = 512r.iter_content进度栏中插入时,在显示下载数据时根本不会加载,但是当我chunk_size = …

python download tqdm

5
推荐指数
1
解决办法
6142
查看次数

标签 统计

download ×1

python ×1

tqdm ×1