forked from shareAI-lab/learn-claude-code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathweather_example.py
More file actions
171 lines (143 loc) · 5.42 KB
/
Copy pathweather_example.py
File metadata and controls
171 lines (143 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
使用OpenWeatherMap API查询天气的示例代码
需要先注册获取API密钥:https://openweathermap.org/api
"""
import requests
import json
import sys
def get_weather_by_city(city_name, api_key, units="metric"):
"""
根据城市名称查询天气
Args:
city_name: 城市名称(如:"Beijing")
api_key: OpenWeatherMap API密钥
units: 温度单位,"metric"为摄氏度,"imperial"为华氏度
Returns:
天气数据的字典
"""
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city_name,
"appid": api_key,
"units": units,
"lang": "zh_cn" # 中文返回
}
try:
response = requests.get(base_url, params=params, timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"请求错误: {e}")
return None
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
return None
def get_weather_by_coords(lat, lon, api_key, units="metric"):
"""
根据经纬度坐标查询天气
Args:
lat: 纬度
lon: 经度
api_key: OpenWeatherMap API密钥
units: 温度单位
Returns:
天气数据的字典
"""
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"lat": lat,
"lon": lon,
"appid": api_key,
"units": units,
"lang": "zh_cn"
}
try:
response = requests.get(base_url, params=params, timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"请求错误: {e}")
return None
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
return None
def display_weather(weather_data):
"""
显示天气信息
Args:
weather_data: 天气数据字典
"""
if not weather_data or "cod" in weather_data and weather_data["cod"] != 200:
print("无法获取天气数据")
if weather_data and "message" in weather_data:
print(f"错误信息: {weather_data['message']}")
return
print("\n=== 天气信息 ===")
print(f"城市: {weather_data.get('name', '未知')}")
if "sys" in weather_data:
country = weather_data["sys"].get("country", "")
print(f"国家: {country}")
if "main" in weather_data:
main = weather_data["main"]
print(f"温度: {main.get('temp', '未知')}°C")
print(f"体感温度: {main.get('feels_like', '未知')}°C")
print(f"最低温度: {main.get('temp_min', '未知')}°C")
print(f"最高温度: {main.get('temp_max', '未知')}°C")
print(f"湿度: {main.get('humidity', '未知')}%")
print(f"气压: {main.get('pressure', '未知')} hPa")
if "weather" in weather_data and len(weather_data["weather"]) > 0:
weather = weather_data["weather"][0]
print(f"天气状况: {weather.get('description', '未知')}")
print(f"天气图标: http://openweathermap.org/img/wn/{weather.get('icon', '')}@2x.png")
if "wind" in weather_data:
wind = weather_data["wind"]
print(f"风速: {wind.get('speed', '未知')} m/s")
if "deg" in wind:
print(f"风向: {wind.get('deg', '未知')}°")
if "clouds" in weather_data:
print(f"云量: {weather_data['clouds'].get('all', '未知')}%")
if "visibility" in weather_data:
visibility = weather_data["visibility"]
if visibility >= 1000:
print(f"能见度: {visibility/1000:.1f} km")
else:
print(f"能见度: {visibility} m")
if "dt" in weather_data:
from datetime import datetime
dt = datetime.fromtimestamp(weather_data["dt"])
print(f"数据时间: {dt.strftime('%Y-%m-%d %H:%M:%S')}")
if "timezone" in weather_data:
offset = weather_data["timezone"] / 3600
print(f"时区: UTC{offset:+g}")
def main():
"""
主函数:演示如何使用API
"""
print("OpenWeatherMap API 天气查询示例")
print("=" * 40)
# 您需要在这里填写您的API密钥
API_KEY = "YOUR_API_KEY_HERE" # 请替换为您的实际API密钥
if API_KEY == "YOUR_API_KEY_HERE":
print("\n⚠️ 请先注册OpenWeatherMap并获取API密钥:")
print(" https://openweathermap.org/api")
print("\n注册步骤:")
print("1. 访问 https://openweathermap.org/api")
print("2. 点击 'Subscribe' 或 'Sign Up'")
print("3. 注册免费账户(Current Weather Data API 免费套餐每天可调用60次)")
print("4. 在控制台获取您的API密钥")
print("\n获取API密钥后,请替换代码中的 'YOUR_API_KEY_HERE'")
return
# 示例:查询北京的天气
print("\n示例1:查询北京天气")
weather_data = get_weather_by_city("Beijing", API_KEY)
if weather_data:
display_weather(weather_data)
# 示例:使用经纬度查询(纽约的坐标)
print("\n" + "=" * 40)
print("示例2:使用经纬度查询纽约天气")
weather_data = get_weather_by_coords(40.7128, -74.0060, API_KEY)
if weather_data:
display_weather(weather_data)
if __name__ == "__main__":
main()