You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
python-project/download/文件下载.py

27 lines
1008 B

import os
from urllib.request import urlopen
from tqdm import tqdm
import requests
import sys
from threading import Thread,Lock
lock = Lock()
def download(url,filename=None):
file_size = int(requests.head(url).headers['Content-Length'])
if not filename:
filename = url.split('/')[-1]
if os.path.exists(filename):
first_byte = os.path.getsize(filename)
print('断点续传中。。。')
else:
first_byte = 0
header = {'Range': 'bytes=%s-%s' % (first_byte, file_size)}
pbar = tqdm(total=file_size,initial=first_byte,unit='B',unit_scale=True,desc=url.split('/')[-1],mininterval=0.5)
result = requests.get(url,headers = header,stream=True)
with open(filename,'ab') as f:
for chunk in result.iter_content(chunk_size=1024):
f.write(chunk)
pbar.update(1024)
pbar.close()
return file_size
if __name__ == '__main__':
download('http://huo.hongjiaozuida.com/20200606/5381_8ab40c11/少年间谍第一季-08.mp4')