Python:添加进度条供下载
我见过许多关于stackoverflow的问题,但没有一个答案给出一个简单优雅的方法Python:添加进度条供下载,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我见过许多关于stackoverflow的问题,但没有一个答案给出一个简单优雅的方法 link = "http://download.thinkbroadband.com/10MB.zip" file_name = "test" with open(file_name, "wb") as f: print('Downloading: {}'.format(file_name)) response = requests.get(link, stream=True)
link = "http://download.thinkbroadband.com/10MB.zip"
file_name = "test"
with open(file_name, "wb") as f:
print('Downloading: {}'.format(file_name))
response = requests.get(link, stream=True)
total_length = response.headers.get('content-length')
if total_length is None:
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)))
sys.stdout.flush()
在下载文件时,我可以获得更多详细信息吗?前面的所有问题都没有一个简单的好答案。为什么要重新发明轮子?使用。按照链接和说明导入TQM并为任何迭代添加进度条。例如:
from tqdm import tqdm
...
for data in tqdm(response.iter_content(chunk_size=4096)):
# additional logic here
...
阅读提供的pypi链接中的示例,将其他信息添加到进度栏中 除非你没有理由不想使用这个,否则很难提出不同的建议(那时你可能也不想使用)好吧,我重新措辞的问题:当我从链接下载时,我想要更多的细节。