我需要下载一个相当大的(~200MB)文件。我想出了如何下载并保存文件here https://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py。最好有一个进度条来知道已经下载了多少。我发现进度条 http://progressbar-2.readthedocs.io/en/latest/usage.html但我不确定如何将两者结合在一起。
这是我尝试过的代码,但它不起作用。
bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
with closing(download_file()) as r:
for i in range(20):
bar.update(i)
我建议你尝试一下tqdm https://github.com/tqdm/tqdm,使用起来非常方便。
下载示例代码requests http://docs.python-requests.org/en/master/图书馆:
from tqdm import tqdm
import requests
url = "http://www.ovh.net/files/10Mb.dat" #big file test
# Streaming, so we can iterate over the response.
response = requests.get(url, stream=True)
total_size_in_bytes= int(response.headers.get('content-length', 0))
block_size = 1024 #1 Kibibyte
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)
with open('test.dat', 'wb') as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
progress_bar.close()
if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
print("ERROR, something went wrong")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)