





import requests
class WeatherAPI:
def __init__(self, key):
self.key = key
self.translation_dict = {
"code": "状态码",
"updateTime": "更新时间",
"fxLink": "天气链接",
"now": "当前天气",
"obsTime": "观测时间",
"temp": "温度",
"feelsLike": "体感温度",
"icon": "天气图标",
"text": "天气描述",
"wind360": "风向角度",
"windDir": "风向",
"windScale": "风力等级",
"windSpeed": "风速",
"humidity": "湿度",
"precip": "降水量",
"pressure": "气压",
"vis": "能见度",
"cloud": "云量",
"dew": "露点",
"daily": "每日天气",
"fxDate": "预报日期",
"sunrise": "日出时间",
"sunset": "日落时间",
"moonrise": "月升时间",
"moonset": "月落时间",
"moonPhase": "月相",
"moonPhaseIcon": "月相图标",
"tempMax": "最高温度",
"tempMin": "最低温度",
"iconDay": "白天天气图标",
"textDay": "白天天气描述",
"iconNight": "夜晚天气图标",
"textNight": "夜晚天气描述",
"wind360Day": "白天风向角度",
"windDirDay": "白天风向",
"windScaleDay": "白天风力等级",
"windSpeedDay": "白天风速",
"wind360Night": "夜晚风向角度",
"windDirNight": "夜晚风向",
"windScaleNight": "夜晚风力等级",
"windSpeedNight": "夜晚风速",
"uvIndex": "紫外线指数",
"license": "许可证信息",
"sources": "数据来源"
}
def _translate_json_keys(self, json_data):
"""
将JSON数据中的键进行翻译
参数:
- json_data (dict/list/其他): 需要进行键翻译的JSON数据
返回值:
- translated_data (dict/list/其他): 翻译后的JSON数据
"""
if isinstance(json_data, dict):
translated_data = {}
for key, value in json_data.items():
translated_key = self.translation_dict.get(key, key)
translated_value = self._translate_json_keys(value)
translated_data[translated_key] = translated_value
return translated_data
elif isinstance(json_data, list):
translated_data = []
for item in json_data:
translated_item = self._translate_json_keys(item)
translated_data.append(translated_item)
return translated_data
else:
return json_data
def get_location_id(self, location, adm='', range='cn'):
"""
获取指定位置的地点ID
参数:
- location (str): 地点名称
- adm (str): 行政区划代码(可选)
- range (str): 查询范围(默认为'cn')
返回值:
- location_id (str): 地点ID
"""
get_location_url = (f'https://geoapi.qweather.com/v2/city/lookup?location={location}&adm={adm}&key='
f'{self.key}&range={range}')
location_res = requests.get(get_location_url).json()
print(f"城市信息: {location_res['location']}")
location_id = location_res['location'][0]['id']
return location_id
def get_weather_forecast(self, location_id, day):
"""
获取指定地点的天气预报
参数:
- location_id (str): 地点ID
- day (str): 预报天数 ('1d', '3d', '7d', '10d', '15d', '30d')
返回值:
- forecast_data (dict): 天气预报数据
"""
get_forecast_url = f'https://devapi.qweather.com/v7/weather/{day}?location={location_id}&key={self.key}'
forecast_res = requests.get(get_forecast_url).json()
forecast_data = self._translate_json_keys(forecast_res)
return forecast_data
def get_weather_now(self, location_id):
"""
获取指定地点的当前天气
参数:
- location_id (str): 地点ID
返回值:
- weather_data (dict): 当前天气数据
"""
get_weather_now_url = f'https://devapi.qweather.com/v7/weather/now?location={location_id}&key={self.key}'
weather_now_res = requests.get(get_weather_now_url).json()
weather_data = self._translate_json_keys(weather_now_res)
return weather_data
if __name__ == '__main__':
# 创建 WeatherAPI 实例
api = WeatherAPI(key='')
# 获取地点ID
location_id = api.get_location_id(location='西安', adm='黑龙江')
# 获取当前天气
current_weather = api.get_weather_now(location_id=location_id)
print(current_weather)
# 获取天气预报
forecast = api.get_weather_forecast(location_id=location_id, day='7d')
print(forecast)