Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 36fe439

Browse files
committed
增加带进度条下载脚本
1 parent b04768c commit 36fe439

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎2.download_with_process_bar.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
__author__ = "eason"
3+
import requests
4+
import os
5+
from tqdm import tqdm
6+
7+
8+
def download_file(url: str, save_path: str)->bool:
9+
"""
10+
下载文件并显示下载进度
11+
:param url: 文件url
12+
:param save_path: 保存路径
13+
:return:
14+
"""
15+
try:
16+
local_filename = os.path.join(save_path, url.split('/')[-1])
17+
r = requests.get(url, stream=True, timeout=60)
18+
# Total size in bytes.
19+
total_size = int(r.headers.get('content-length', 0))
20+
# print url, total_size
21+
with open(local_filename, 'wb') as f:
22+
for chunk in tqdm(r.iter_content(), total=total_size, unit='B', unit_scale=True, desc=url.split('/')[-1],
23+
leave=True, ncols=100):
24+
f.write(chunk)
25+
return True
26+
except Exception as e:
27+
print(repr(e))
28+
return False
29+
30+
if __name__ == "__main__":
31+
download_file(url="https://dldir1.qq.com/qqfile/qq/TIM2.1.0/22747/TIM2.1.0.exe", save_path=r"C:\Users\eason\Desktop\test")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /