简单进度条打印
from alive_progress import alive_bar
import time
with alive_bar(3000) as bar:
for i in range(3000):
bar()
time.sleep(0.01)
下载任务
from alive_progress import alive_bar
import time
import requests
import os
def download():
#url = 'http://mirrors.163.com/centos/8.3.2011/isos/x86_64/CentOS-8.3.2011-x86_64-boot.iso'
url = 'https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/PCQQ2021.exe'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
filename = url.split('/')[-1]
totalSize = int(requests.head(url,headers=header).headers['Content-Length'])
if os.path.exists(filename):
fileSize = os.path.getsize(filename)
if fileSize < totalSize:
print('断点续传中。。。')
elif fileSize == totalSize:
print('文件已存在')
exit(0)
else:
file_size = 0
header = {'Range': 'bytes=%s-%s' % (fileSize, totalSize)}
result = requests.get(url, headers=header, stream=True)
with open(filename, 'ab') as f:
with alive_bar(int((totalSize - fileSize)/1024)) as bar:
for i in result.iter_content(chunk_size=1024):
f.write(i)
bar()
download()
from alive_progress import alive_bar
import math
import requests
import os
class Download():
def __init__(self, urlPath=None):
self.urlPath = urlPath
self.filename = urlPath.split('/')[-1]
self.header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
def download(self):
self.header['Range'] = 'bytes=%s-%s' % (self.fileSize, self.totalSize)
self.result = requests.get(url=self.urlPath, headers=self.header, stream=True)
def progress(self):
with alive_bar(total=math.ceil((self.totalSize - self.fileSize) / 1024), title=self.filename, title_length=10, force_tty=True) as bar:
with open(self.filename, 'wb') as f:
for i in self.result.iter_content(chunk_size=1024):
f.write(i)
bar()
def checkPath(self):
self.totalSize = int(requests.head(url=self.urlPath, headers=self.header).headers['Content-Length'])
if os.path.exists(self.filename):
self.fileSize = os.path.getsize(self.filename)
if self.fileSize < self.totalSize:
print(f'文件{self.filename}断点续传中')
else:
print('文件已存在')
return ''
else:
self.fileSize = 0
def run(self):
self.checkPath()
self.download()
self.progress()
if __name__ == '__main__':
with open('./url.txt','r') as f:
urls = f.read().splitlines()
for url in urls:
if not url:
continue
s = Download(urlPath=url)
s.run()