import requests import json # 接口地址(建议加上你的 api_key,否则可能无法访问) url = "https://api.apihubs.cn/holiday/get" params = { "year": 2026, "page": 1, "size": 370 # "api_key": "your_api_key_here" # ⚠️ 如果接口坞要求认证,请取消注释并填入你的 key } try: response = requests.get(url, params=params) response.raise_for_status() # 检查 HTTP 错误 # 解析 JSON 数据 data = response.json() # 保存到文件 with open("holidays_2026.json", "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) print("✅ 数据已成功保存到 holidays_2026.json") except requests.exceptions.RequestException as e: print(f"❌ 网络请求失败: {e}") except json.JSONDecodeError: print("❌ 返回内容不是有效的 JSON 格式") print("原始响应:", response.text) except Exception as e: print(f"❌ 发生未知错误: {e}")