import requests import json import sys from datetime import datetime # 年份:命令行参数 > 当前年份 if len(sys.argv) > 1: year = int(sys.argv[1]) else: year = datetime.now().year url = "https://api.apihubs.cn/holiday/get" params = { "year": year, "page": 1, "size": 370 # "api_key": "your_api_key_here" # 如果接口要求认证,请取消注释并填入你的 key } try: response = requests.get(url, params=params) response.raise_for_status() data = response.json() filename = f"holidays_{year}.json" with open(filename, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) print(f"数据已成功保存到 {filename}") except requests.exceptions.RequestException as e: print(f"网络请求失败: {e}") except json.JSONDecodeError: print("返回内容不是有效的 JSON 格式") print("原始响应:", response.text) except Exception as e: print(f"发生未知错误: {e}")