运行时依赖
安装命令
点击复制技能文档
中国天气查询工具,支持实时天气、天气预报、空气质量、生活指数,提供丰富的数据和精美的排版输出。 功能 🌤️ 实时天气: 当前天气状况、温度、湿度、风向 📅 天气预报: 7天/15天天气预报 🌫️ 空气质量: AQI指数、PM2.5、PM10 🧥 生活指数: 穿衣、紫外线、运动、洗车指数 🔄 API降级: 多个免费API自动切换 🎨 精美排版: 专业格式输出 🌍 多语言: 中英文支持 触发条件 "查天气" / "Check weather" "明天天气怎么样" / "What's the weather tomorrow" "北京天气" / "Beijing weather" "空气质量" / "Air quality" "天气预报" / "Weather forecast" "china-weather" API 策略 (接口策略) 认证方式说明 API 认证方式 免费额度 配置要求 wttr.in 无需认证 无限制 ❌ 无需任何配置 和风天气 API Host + Key 1000次/天 ✅ 需要API_HOST + API_KEY OpenWeatherMap API Key 1000次/天 ✅ 需要API_KEY + 绑定信用卡 心知天气 API Key 无限制 ✅ 需要API_KEY 推荐:优先使用wttr.in(无需任何配置,免费无限制) 降级策略 # 优先级:无需配置 → 需要配置 API_CHAIN = [ {"name": "wttr", "requires_key": False}, {"name": "qweather", "requires_key": True}, {"name": "openweathermap", "requires_key": True} ] 和风天气配置说明(重要) 和风天气从2026年起不再支持公共API地址,必须使用专属API Host: # 环境变量配置 export QWEATHER_API_HOST="你的API_HOST" # 如:abc1234xyz.def.qweatherapi.com export QWEATHER_API_KEY="你的API_KEY" # 正确的API调用 curl "https://${QWEATHER_API_HOST}/v7/weather/now?location=101010100&key=${QWEATHER_API_KEY}" 注意:响应使用gzip压缩,需要添加--compressed参数或在代码中处理 降级策略 API_CHAIN = [ {"name": "qweather", "priority": 1, "fallback": True}, {"name": "seniverse", "priority": 2, "fallback": True}, {"name": "openweathermap", "priority": 3, "fallback": True}, {"name": "wttr", "priority": 4, "fallback": False} ] def get_weather_with_fallback(city): """Try APIs in priority order, fallback on failure""" for api in API_CHAIN: try: result = call_api(api["name"], city) if result: return result except Exception as e: if api["fallback"]: continue else: raise return None 步骤 1:安装依赖 pip install requests 步骤 2:天气查询脚本 python3 << 'PYEOF' import os import requests import json from datetime import datetime class WeatherService: def __init__(self): self.apis = { 'qweather': QWeatherAPI(), 'seniverse': SeniverseAPI(), 'openweathermap': OpenWeatherMapAPI(), 'wttr': WttrAPI() } def get_weather(self, city, days=7): """Get weather with fallback strategy""" for name, api in self.apis.items(): try: result = api.get_weather(city, days) if result: result['source'] = name return result except Exception as e: print(f"⚠️ {name} failed: {e}") continue return None def format_weather(self, data, lang='zh'): """Format weather data beautifully""" if lang == 'zh': return self._format_chinese(data) else: return self._format_english(data) def _format_chinese(self, data): """Chinese format output""" output = [] output.append(f"┌{'─'50}┐") output.append(f"│ 🌤️ {data['city']}天气预报") output.append(f"└{'─'50}┘") output.append("") # 当前天气 output.append(f"📍 当前天气") output.append(f"├─ 🌡️ 温度: {data['current']['temp']}°C (体感 {data['current']['feels_like']}°C)") output.append(f"├─ 🌤️ 天气: {data['current']['weather']}") output.append(f"├─ 💧 湿度: {data['current']['humidity']}%") output.append(f"├─ 🌬️ 风向: {data['current']['wind_dir']} {data['current']['wind_speed']}km/h") output.append(f"└─ 👁️ 能见度: {data['current']['visibility']}km") output.append("") # 空气质量 if 'aqi' in data: output.append(f"🌫️ 空气质量") aqi = data['aqi'] aqi_level = self._get_aqi_level(aqi['value']) output.append(f"├─ AQI: {aqi['value']} ({aqi_level})") output.append(f"├─ PM2.5: {aqi.get('pm25', 'N/A')}μg/m³") output.append(f"├─ PM10: {aqi.get('pm10', 'N/A')}μg/m³") output.append(f"└─ 首要污染物: {aqi.get('primary', 'N/A')}") output.append("") # 未来预报 output.append(f"📅 未来预报") for day in data.get('forecast', [])[:7]: output.append(f"├─ {day['date']}: {day['weather']} {day['temp_min']}~{day['temp_max']}°C") output.append("") # 生活指数 if 'indices' in data: output.append(f"🧥 生活指数") for idx in data['indices'][:4]: output.append(f"├─ {idx['name']}: {idx['level']}") return '\n'.join(output) def _format_english(self, data): """English format output""" output = [] output.append(f"┌{'─'50}┐") output.append(f"│ 🌤️ {data['city']} Weather Forecast") output.append(f"└{'─'50}┘") output.append("") # Current weather output.append(f"📍 Current Weather") output.append(f"├─ 🌡️ Tempe