本帖最后由 额微粒波地 于 2023-6-14 15:59 编辑
[Python] 纯文本查看 复制代码 # 吾爱破解 - 额微粒波地
# pip install requests
# pip install json
# pip install tqdm
import json
import requests
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
url_data = 'https://api.smmdb.net/courses2/download/{}'
json_data = json.load(open('smmdb.json', 'r', encoding='utf-8'))
def download(url, course):
with requests.get(url, stream=True) as response:
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
with open('{}.tar'.format(course['course']['header']['title']), 'wb') as f, tqdm(total=total_size, unit='B', unit_scale=True) as progress_bar:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
progress_bar.update(len(chunk))
def download_all(course_list):
# max_workers 最大线程为4
with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
for course in course_list:
url = url_data.format(course['id'])
futures.append(executor.submit(download, url, course))
for future in tqdm(futures, total=len(futures)):
course = future.args[1]
print('下载完成: {}'.format(
course['course']['header']['title']))
future.result()
download_all(json_data)
print('所有文件下载完成.')
json文件:https://api.smmdb.net/courses2?limit=120&skip=0 复制页面数据保存为smmdb.json 和这个py文件放在同一目录下 亲测可下载
|