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/spider/文件下载2.py

51 lines
1.7 KiB

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()