import os import requests from alist import AlistClient from urllib.parse import unquote, urlparse def download_file(url, local_path): response = requests.get(url, stream=True) total_size = int(response.headers.get('content-length', 0)) downloaded_size = 0 with open(local_path, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) downloaded_size += len(chunk) if total_size > 0: percent = downloaded_size * 100 / total_size print(f"\rDownloading: {percent:.1f}% ({downloaded_size}/{total_size} bytes)", end='') print() # 换行 def create_local_path(base_url, url): parsed = urlparse(url) path = parsed.path # 去掉/d/前缀 if path.startswith('/d/'): path = path[3:] # 去掉文件名后的参数 path = path.split('?')[0] print(path) # 创建本地路径 local_path = os.path.join('c:/videos/', path) print(local_path) os.makedirs(os.path.dirname(local_path), exist_ok=True) return local_path client = AlistClient("http://localhost:5244", "admin", "123456") fs = client.fs fs.chdir("/") for path in fs.iter(max_depth=-1): if path.name.endswith(".mp4"): decoded_url = unquote(path.url) if '第2周' in decoded_url: print(f"Downloading: {decoded_url}") local_path = create_local_path("http://localhost:5244", decoded_url) download_file(decoded_url, local_path) print(f"Saved to: {local_path}") else: print(f"Skipping: {decoded_url} (does not contain '第2周')")
使用之前
pip install python-alist
标签:decoded,百度网,python,Alist,url,print,path,local,size From: https://www.cnblogs.com/meetrice/p/18664092