
import requests
import time
def DownloadFile(url,save_path):
download_size = 0
download_last_size = 0
response = requests.get(url, stream=True)
chunk_size = 1024
total_size = int(response.headers['content-length'])
if response.status_code ==200:
print('[文件大小]:%0.2f MB' % (total_size/chunk_size/1024))
with open(save_path, "wb") as file:
start_time = time.time()
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
download_size+=len(data)
if time.time()-start_time>2:
speed = (download_size-download_last_size)/1024/1024/2
download_last_size = download_size
print("下载速度:%.2fM/s" % speed)
start_time = time.time()
print('\r'+'[下载进度]:%s%.2f%%' % ('>'*int(download_size/total_size), float(download_size/total_size*100)), end='')
url = 'http://dx.198424.com/soft1/sql2000-kb884525-sp4.rar'
DownloadFile(url,'D:\\test.mp3')