import requestsdef wind_dir_to_text(degrees): """将风向度数转换为中文方位""" if degrees is None: return "未知" dirs = ["北风", "东北风", "东风", "东南风", "南风", "西南风", "西风", "西北风"] idx = round(degrees / 45) % 8 return dirs[idx]def weather_code_to_text(code): """将WMO天气代码转换为中文描述""" # 参考 Open-Meteo 天气代码(基于 WMO) weather_map = { 0: "晴", 1: "基本晴", 2: "局部多云", 3: "阴", 45: "雾", 48: "沉积雾", 51: "轻毛毛雨", 53: "中毛毛雨", 55: "浓毛毛雨", 56: "冻毛毛雨", 57: "强冻毛毛雨", 61: "小雨", 63: "中雨", 65: "大雨", 66: "冻雨", 67: "强冻雨", 71: "小雪", 73: "中雪", 75: "大雪", 77: "雪粒", 80: "小阵雨", 81: "中阵雨", 82: "大阵雨", 85: "小雪阵", 86: "大雪阵", 95: "雷暴", 96: "雷暴伴小冰雹", 99: "雷暴伴大冰雹", } return weather_map.get(code, f"未知({code})")def get_current_weather(lat, lon): """获取指定经纬度的全部当前实况气象数据,输出中文""" url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": lat, "longitude": lon, "current": [ "temperature_2m", "relative_humidity_2m", "apparent_temperature", "is_day", "precipitation", "rain", "showers", "snowfall", "weather_code", "cloud_cover", "pressure_msl", "surface_pressure", "wind_speed_10m", "wind_direction_10m", "wind_gusts_10m" ], "wind_speed_unit": "ms", "timezone": "Asia/Shanghai" } # 字段名中文映射(key -> 中文名称) field_name_map = { "temperature_2m": "温度", "relative_humidity_2m": "相对湿度", "apparent_temperature": "体感温度", "is_day": "白天/夜晚", "precipitation": "总降水量", "rain": "雨量", "showers": "阵雨量", "snowfall": "降雪量", "weather_code": "天气现象", "cloud_cover": "云量", "pressure_msl": "海平面气压", "surface_pressure": "地表气压", "wind_speed_10m": "风速", "wind_direction_10m": "风向", "wind_gusts_10m": "阵风风速", "time": "数据时间" } # 单位映射 unit_map = { "temperature_2m": "°C", "relative_humidity_2m": "%", "apparent_temperature": "°C", "precipitation": "mm", "rain": "mm", "showers": "mm", "snowfall": "cm", "cloud_cover": "%", "pressure_msl": "hPa", "surface_pressure": "hPa", "wind_speed_10m": "m/s", "wind_gusts_10m": "m/s", "wind_direction_10m": "", "weather_code": "", "is_day": "", "time": "" } try: response = requests.get(url, params=params) response.raise_for_status() data = response.json() if "current" in data: current = data["current"] print(f"经纬度: {lat}, {lon}") # 获取时区转换后的本地时间(API 返回的是 UTC 时间,但设置了 timezone 后 current 中的 time 已经是本地时间字符串) print(f"数据时间: {current.get('time', 'N/A')}") print("-" * 40) for key, value in current.items(): # 跳过 time,已经单独打印 if key == "time": continue # 获取中文名称 ch_name = field_name_map.get(key, key) # 处理特殊值的转换 if key == "weather_code": value = weather_code_to_text(value) unit = "" elif key == "is_day": value = "白天" if value == 1 else "夜晚" unit = "" elif key == "wind_direction_10m": # 风向:同时显示度数和方位文字 direction_text = wind_dir_to_text(value) value = f"{value}° ({direction_text})" unit = "" else: unit = unit_map.get(key, "") # 格式化数值(保留一位小数) if isinstance(value, (int, float)) and key not in ["weather_code", "is_day"]: value = f"{value:.1f}" print(f"{ch_name}: {value}{unit}".strip()) else: print("响应中未找到 'current' 字段,原始返回:", data) except requests.exceptions.RequestException as e: print(f"网络请求错误: {e}") except Exception as e: print(f"解析数据时发生错误: {e}")# 调用示例:经度 117.25, 纬度 34.75get_current_weather(34.75, 117.25)