1, 下载,肯定要看进度
优秀的 progress bar
tqdm
安装sudo python3 -m pip install tqdm
1.1, 加环境变量#!/usr/bin/python3
from tqdm import tqdm
import requests上一步安装 tqdm 的是 python3 ,
python3 关联到了 tqdm 这个库,
不加环境变量,
走默认的 python 2.7,不是在 python 2.7 上,安装的这个库 tqdm
2 , 下载代码
这个文件,700 M 左右
走文件流,下载的速度,比 Chrome 快多了
走 Chrome 下载,老是失败#!/usr/bin/python3
from tqdm import tqdm
import requests
url = 'https://static.realm.io/downloads/swift/realm-swift-10.1.1.zip'
# 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)
path = '/Users/xx/Desktop/Papr-develop/realm-swift-10.1.1.zip'
print("总量:")
print(total_size_in_bytes)
with open(path, '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")