import requests import os from pathlib import Path def get_vnames(url): try: response = requests.get(url) if response.status_code == 200: return response.json() else: return f"请求失败,状态码:{response.status_code}" except requests.exceptions.RequestExecption as e: return f"请求发生错误:{e}" def get_videos(file_path, local_path): p = Path(file_path) try: response = requests.get(file_path, stream=True) if response.status_code == 200: with open(local_path, "wb") as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) print(f"{p.name}下载并保存成功") else: print(f"请求失败,状态码为:{response.status_code}") except requests.exceptions.RequestException as e: print(f"请求发生错误:{e}") if __name__ == "__main__": current_dir = os.path.dirname(os.path.abspath(__file__)) local_dir = os.path.join(current_dir, "downloads") url = "http://47.120.70.16:8080/" names_url = url + "show/videos_name" msg = get_vnames(names_url) video_names = msg["file_names"] for name in video_names: local_path = os.path.join(local_dir, name) file_url = url + os.path.join("download", name) get_videos(file_url, local_path)