一、动手体验:5min实现第一个智能体
在本案例中,我们的目标是构建一个能处理分步任务的智能旅行助手。需要解决的用户任务定义为:"你好,请帮我查询一下今天北京的天气,然后根据天气推荐一个合适的旅游景点。"要完成这个任务,智能体必须展现出清晰的逻辑规划能力。它需要先调用天气查询工具,并将获得的观察结果作为下一步的依据。在下一轮循环中,它再调用景点推荐工具,从而得出最终建议。
为了能从 Python 程序中访问网络 API,我们需要一个 HTTP 库。
requests是 Python 社区中最流行、最易用的选择。tavily-python是一个强大的 AI 搜索 API 客户端,用于获取实时的网络搜索结果,可以在官网注册后获取 API。openai是 OpenAI 官方提供的 Python SDK,用于调用 GPT 等大语言模型服务。角色拆解
requests------ "通信员" (基础工具)
定义: 这是一个 HTTP 库。
作用: 它是 Python 用来"上网"的最基本工具。就像你的浏览器(Chrome/Edge)用来访问网页一样,Python 程序用
requests来向互联网上的服务器发送请求并获取数据。地位: Python 社区的标配,几乎所有涉及网络的程序都会用到它。
openai------ "大脑" (处理核心)
定义: OpenAI 的官方 Python 软件开发工具包 (SDK)。
作用: 它让你的程序能连接到 OpenAI 的服务器,使用 GPT-3.5 或 GPT-4 模型。
能力: 负责理解用户的意图、进行逻辑推理、总结内容、回答问题。它是整个程序的"智力"来源。
tavily-python------ "眼睛/搜查员" (信息获取)
定义: 专门为 AI 设计的搜索引擎接口。
作用: GPT 的知识是有截止日期的(比如它可能不知道今天的新闻),而 Tavily 可以实时搜索互联网。
特点: 与普通的 Google 搜索不同,Tavily 返回的是经过整理的、适合 AI 阅读的文本数据,而不是复杂的网页 HTML 代码,这大大提高了 AI 的处理效率。
它们如何协同工作?
这段文字通常出现在"AI Agent(智能体)"开发的教程或文档中。当你把这三个工具组合在一起时,工作流程通常是这样的:
用户提问: "今天旧金山的天气怎么样?"
openai(大脑) 分析: GPT 发现自己不知道今天的实时天气,判断需要搜索网络。
tavily-python(搜查员) 执行: 接收指令,去互联网上搜索"旧金山 实时天气",并把搜索结果带回来。
requests(通信员): 在幕后支持这些数据传输(虽然openai和tavily库内部封装好了网络请求,但复杂的自定义功能常需配合requests使用)。最终回答: GPT 结合搜索到的结果,生成最终答案:"今天旧金山多云转晴,气温..."
特性 pip install conda install 角色 Python 的官方包管理器。 跨平台的包管理器 + 环境管理器。 仓库来源 PyPI (Python Package Index)。拥有最全的 Python 库(超过 30 万个)。 Anaconda Repository 或 Conda-forge。库的数量较少,经过严格筛选和测试。 管理范围 只管理 Python 库。 不仅管理 Python 库,还能管理 C/C++ 库、R 语言包等。 依赖处理 有时会因为缺少系统级的 C 库(如编译器)而安装失败。 擅长处理复杂的底层依赖(比如 NumPy, TensorFlow 需要的 C 库),直接下载编译好的二进制文件,更稳定。 适用场景 几乎所有 Python 项目,特别是由于库太新、Conda 没有收录时。 科学计算、深度学习环境搭建(避免复杂的编译错误)。
1.1 准备工作
python
pip install requests tavily-python openai
1.2 指令模板
驱动真实 LLM 的关键在于提示工程(Prompt Engineering) 。我们需要设计一个"指令模板",告诉 LLM 它应该扮演什么角色、拥有哪些工具、以及如何格式化它的思考和行动。这是我们智能体的"说明书",它将作为system_prompt传递给 LLM。
python
AGENT_SYSTEM_PROMPT = """
你是一个智能旅行助手。你的任务是分析用户的请求,并使用可用工具一步步地解决问题。
# 可用工具:
- `get_weather(city: str)`: 查询指定城市的实时天气。
- `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。
# 行动格式:
你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:
Thought: [这里是你的思考过程和下一步计划]
Action: 你决定采取的行动,必须是以下格式之一:
- `function_name(arg_name="arg_value")`:调用一个可用工具。
- `Finish[最终答案]`:当你认为已经获得最终答案时。
- 当你收集到足够的信息,能够回答用户的最终问题时,你必须在Action:字段后使用 Finish[最终答案] 来输出最终答案。
请开始吧!
"""
1.3 工具1:查询天气
python
import requests
def get_weather(city: str) -> str:
"""
通过调用 wttr.in API 查询真实的天气信息。
"""
# API端点,我们请求JSON格式的数据
url = f"https://wttr.in/{city}?format=j1"
try:
# 发起网络请求
response = requests.get(url)
# 检查响应状态码是否为200 (成功)
response.raise_for_status()
# 解析返回的JSON数据
data = response.json()
# 提取当前天气状况
current_condition = data['current_condition'][0]
weather_desc = current_condition['weatherDesc'][0]['value']
temp_c = current_condition['temp_C']
# 格式化成自然语言返回
return f"{city}当前天气:{weather_desc},气温{temp_c}摄氏度"
except requests.exceptions.RequestException as e:
# 处理网络错误
return f"错误:查询天气时遇到网络问题 - {e}"
except (KeyError, IndexError) as e:
# 处理数据解析错误
return f"错误:解析天气数据失败,可能是城市名称无效 - {e}"
1.3.1 要点分析------f-string(格式化字符串)
核心:f代表"Format"(格式化),它的作用是让字符串里的"占位符"变成"活的"。f"..."是告诉Python:"这个字符串里有变量(用花括号包着的),请帮我把它们换成真正的值,然后再拿去用。"
如果没有 f
python
city = "Beijing"
# 错误写法: 没有 f
url = "https://wttr.in/{city}?format=j1"
print(url)
# 输出结果:https://wttr.in/{city}?format=j1
后果: Python会把你写的{city}当作普通的文字字符。当你拿着这个地址去访问往回走那是,网站会以为你要查询一个叫"{city}"的城市,结果当然是查不到或者报错
有 f
python
city = "Beijing"
url = f"https://wttr.in/{city}?format=j1"
# Python 在后台做了这件事:
# 1. 看到 {city}
# 2. 去找 city 变量的值是什么 (是 "Beijing")
# 3. 把 "Beijing" 塞进去替换掉 {city}
print(url)
# 输出结果: https://wttr.in/Beijing?format=j1
后果:生成了一个动态的、正确的网址。如果下次city变成了"Shanghai",生成的网址就会自动变成.../Shanghai...
1.3.2 要点分析------response.raise_for_status()
核心: 用来检查相应状态码是否为200(成功),成功就继续执行下一段代码,不成功就抛出异常错误!
**意义:**在于"早发现,早治疗"
python
# 发起网络请求
response = requests.get(url)
# 检查响应状态码是否为200 (成功)
response.raise_for_status()
隐性失败(Silent Failure)
如果没有这行代码,后果很严重,这种情况叫做"隐性失败"
场景模拟:假设你要查找一个不存在的城市,比如get_weather(city="Hogwarts")
情况A:没有这行代码
python
response = requests.get(".../Hogwarts...")
# 此时服务器返回了 404(找不到),并不是 200,
# 但是 requests 默认不会报错, response 变量里存的是一段 HTML 网页代码,写着"404 Not Found"
data = response.json()
# 崩溃!
# 原因: 程序试图把"404 Not Found" 的 HTML 网页当作 JSON 数据来解析
# 报错信息: json.decoder.JSONDecodeError
困惑:看到报错会想:"JSON 解析错误? 是不是我提取数据的逻辑写错了?"(其实不是,是因为你根本没拿到数据,但程序没告诉你)
情况B:有这行代码
python
response = requests.get(".../Hogwarts...")
response.raise_for_status()
# 报警!
# 发现状态是 404
# 直接抛出 requests.exceptions.HTTPError: 404 Client Error
不会存在情况A这种困惑了,而是看到报错直接写着404 Error,你立刻就明白了:"哦,原来是这个城市的名字写错了,或者是网站挂了。"
1.3.3 要点分析------如何获得一个网站的json格式
方法一:用Python print 出来
如果你已经拿到了API地址(eg: wttr.in),最直接的办法就是让Python把拿到的数据打印出来,但是普通的print() 打印出来的是一坨乱糟糟的文字,很难看清结构
python
{'current_condition': [{'FeelsLikeC': '-5', 'FeelsLikeF': '23', 'cloudcover': '0', 'humidity': '58', 'localObsDateTime': '2026-01-18 01:30 PM', 'observation_time': '05:30 AM', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1029', 'pressureInches': '30', 'temp_C': '-4', 'temp_F': '25', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'S', 'winddirDegree': '180', 'windspeedKmph': '4', 'windspeedMiles': '2'}], 'nearest_area': [{'areaName': [{'value': 'Beijing'}], 'country': [{'value': 'China'}], 'latitude': '39.929', 'longitude': '116.388', 'population': '7480601', 'region': [{'value': 'Beijing'}], 'weatherUrl': [{'value': ''}]}], 'request': [{'query': 'Lat 39.91 and Lon 116.39', 'type': 'LatLon'}], 'weather': [{'astronomy': [{'moon_illumination': '1', 'moon_phase': 'New Moon', 'moonrise': '07:24 AM', 'moonset': '04:31 PM', 'sunrise': '07:33 AM', 'sunset': '05:17 PM'}], 'avgtempC': '-5', 'avgtempF': '22', 'date': '2026-01-18', 'hourly': [{'DewPointC': '-12', 'DewPointF': '11', 'FeelsLikeC': '-11', 'FeelsLikeF': '13', 'HeatIndexC': '-7', 'HeatIndexF': '20', 'WindChillC': '-11', 'WindChillF': '13', 'WindGustKmph': '11', 'WindGustMiles': '7', 'chanceoffog': '0', 'chanceoffrost': '86', 'chanceofhightemp': '0', 'chanceofovercast': '90', 'chanceofrain': '100', 'chanceofremdry': '0', 'chanceofsnow': '63', 'chanceofsunshine': '0', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '100', 'diffRad': '0.0', 'humidity': '68', 'precipInches': '0.0', 'precipMM': '0.1', 'pressure': '1030', 'pressureInches': '30', 'shortRad': '0.0', 'tempC': '-7', 'tempF': '20', 'time': '0', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '326', 'weatherDesc': [{'value': 'Light snow'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'E', 'winddirDegree': '97', 'windspeedKmph': '9', 'windspeedMiles': '5'}, {'DewPointC': '-10', 'DewPointF': '14', 'FeelsLikeC': '-11', 'FeelsLikeF': '12', 'HeatIndexC': '-7', 'HeatIndexF': '19', 'WindChillC': '-11', 'WindChillF': '12', 'WindGustKmph': '9', 'WindGustMiles': '6', 'chanceoffog': '0', 'chanceoffrost': '86', 'chanceofhightemp': '0', 'chanceofovercast': '93', 'chanceofrain': '100', 'chanceofremdry': '0', 'chanceofsnow': '72', 'chanceofsunshine': '0', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '100', 'diffRad': '0.0', 'humidity': '80', 'precipInches': '0.0', 'precipMM': '0.1', 'pressure': '1031', 'pressureInches': '30', 'shortRad': '0.0', 'tempC': '-7', 'tempF': '19', 'time': '300', 'uvIndex': '0', 'visibility': '5', 'visibilityMiles': '3', 'weatherCode': '332', 'weatherDesc': [{'value': 'Moderate snow'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'E', 'winddirDegree': '88', 'windspeedKmph': '7', 'windspeedMiles': '4'}, {'DewPointC': '-10', 'DewPointF': '13', 'FeelsLikeC': '-11', 'FeelsLikeF': '12', 'HeatIndexC': '-8', 'HeatIndexF': '18', 'WindChillC': '-11', 'WindChillF': '12', 'WindGustKmph': '9', 'WindGustMiles': '5', 'chanceoffog': '0', 'chanceoffrost': '85', 'chanceofhightemp': '0', 'chanceofovercast': '90', 'chanceofrain': '100', 'chanceofremdry': '0', 'chanceofsnow': '66', 'chanceofsunshine': '0', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '100', 'diffRad': '0.0', 'humidity': '81', 'precipInches': '0.0', 'precipMM': '0.1', 'pressure': '1031', 'pressureInches': '30', 'shortRad': '0.0', 'tempC': '-8', 'tempF': '18', 'time': '600', 'uvIndex': '0', 'visibility': '5', 'visibilityMiles': '3', 'weatherCode': '332', 'weatherDesc': [{'value': 'Moderate snow'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'E', 'winddirDegree': '88', 'windspeedKmph': '6', 'windspeedMiles': '4'}, {'DewPointC': '-12', 'DewPointF': '10', 'FeelsLikeC': '-9', 'FeelsLikeF': '16', 'HeatIndexC': '-7', 'HeatIndexF': '19', 'WindChillC': '-9', 'WindChillF': '16', 'WindGustKmph': '5', 'WindGustMiles': '3', 'chanceoffog': '0', 'chanceoffrost': '85', 'chanceofhightemp': '0', 'chanceofovercast': '83', 'chanceofrain': '0', 'chanceofremdry': '0', 'chanceofsnow': '82', 'chanceofsunshine': '0', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '100', 'diffRad': '29.8', 'humidity': '68', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1033', 'pressureInches': '31', 'shortRad': '62.0', 'tempC': '-7', 'tempF': '19', 'time': '900', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '311', 'weatherDesc': [{'value': 'Light freezing rain'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'ESE', 'winddirDegree': '119', 'windspeedKmph': '4', 'windspeedMiles': '2'}, {'DewPointC': '-12', 'DewPointF': '10', 'FeelsLikeC': '-6', 'FeelsLikeF': '21', 'HeatIndexC': '-5', 'HeatIndexF': '24', 'WindChillC': '-6', 'WindChillF': '21', 'WindGustKmph': '4', 'WindGustMiles': '3', 'chanceoffog': '0', 'chanceoffrost': '84', 'chanceofhightemp': '0', 'chanceofovercast': '94', 'chanceofrain': '0', 'chanceofremdry': '85', 'chanceofsnow': '0', 'chanceofsunshine': '16', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '82', 'diffRad': '97.3', 'humidity': '54', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1032', 'pressureInches': '30', 'shortRad': '240.7', 'tempC': '-5', 'tempF': '24', 'time': '1200', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'S', 'winddirDegree': '170', 'windspeedKmph': '4', 'windspeedMiles': '2'}, {'DewPointC': '-12', 'DewPointF': '10', 'FeelsLikeC': '-3', 'FeelsLikeF': '26', 'HeatIndexC': '-3', 'HeatIndexF': '26', 'WindChillC': '-3', 'WindChillF': '26', 'WindGustKmph': '3', 'WindGustMiles': '2', 'chanceoffog': '0', 'chanceoffrost': '82', 'chanceofhightemp': '0', 'chanceofovercast': '42', 'chanceofrain': '0', 'chanceofremdry': '94', 'chanceofsnow': '0', 'chanceofsunshine': '81', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '55', 'diffRad': '95.2', 'humidity': '49', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1031', 'pressureInches': '30', 'shortRad': '360.6', 'tempC': '-3', 'tempF': '26', 'time': '1500', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'WSW', 'winddirDegree': '242', 'windspeedKmph': '3', 'windspeedMiles': '2'}, {'DewPointC': '-12', 'DewPointF': '11', 'FeelsLikeC': '-4', 'FeelsLikeF': '26', 'HeatIndexC': '-4', 'HeatIndexF': '26', 'WindChillC': '-4', 'WindChillF': '26', 'WindGustKmph': '3', 'WindGustMiles': '2', 'chanceoffog': '0', 'chanceoffrost': '81', 'chanceofhightemp': '0', 'chanceofovercast': '82', 'chanceofrain': '0', 'chanceofremdry': '86', 'chanceofsnow': '0', 'chanceofsunshine': '17', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '84', 'diffRad': '51.5', 'humidity': '53', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1033', 'pressureInches': '30', 'shortRad': '156.1', 'tempC': '-4', 'tempF': '26', 'time': '1800', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'SW', 'winddirDegree': '235', 'windspeedKmph': '2', 'windspeedMiles': '1'}, {'DewPointC': '-11', 'DewPointF': '12', 'FeelsLikeC': '-4', 'FeelsLikeF': '25', 'HeatIndexC': '-4', 'HeatIndexF': '25', 'WindChillC': '-4', 'WindChillF': '25', 'WindGustKmph': '3', 'WindGustMiles': '2', 'chanceoffog': '0', 'chanceoffrost': '81', 'chanceofhightemp': '0', 'chanceofovercast': '34', 'chanceofrain': '0', 'chanceofremdry': '92', 'chanceofsnow': '0', 'chanceofsunshine': '88', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '44', 'diffRad': '0.0', 'humidity': '58', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1034', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-4', 'tempF': '25', 'time': '2100', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'WSW', 'winddirDegree': '251', 'windspeedKmph': '2', 'windspeedMiles': '1'}], 'maxtempC': '-3', 'maxtempF': '26', 'mintempC': '-8', 'mintempF': '18', 'sunHour': '8.5', 'totalSnow_cm': '0.5', 'uvIndex': '0'}, {'astronomy': [{'moon_illumination': '0', 'moon_phase': 'Waxing Crescent', 'moonrise': '08:01 AM', 'moonset': '05:38 PM', 'sunrise': '07:33 AM', 'sunset': '05:18 PM'}], 'avgtempC': '-5', 'avgtempF': '22', 'date': '2026-01-19', 'hourly': [{'DewPointC': '-10', 'DewPointF': '14', 'FeelsLikeC': '-4', 'FeelsLikeF': '24', 'HeatIndexC': '-4', 'HeatIndexF': '24', 'WindChillC': '-4', 'WindChillF': '24', 'WindGustKmph': '2', 'WindGustMiles': '1', 'chanceoffog': '0', 'chanceoffrost': '82', 'chanceofhightemp': '0', 'chanceofovercast': '38', 'chanceofrain': '0', 'chanceofremdry': '89', 'chanceofsnow': '0', 'chanceofsunshine': '87', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '39', 'diffRad': '0.0', 'humidity': '63', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1034', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-4', 'tempF': '24', 'time': '0', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'WNW', 'winddirDegree': '282', 'windspeedKmph': '1', 'windspeedMiles': '1'}, {'DewPointC': '-11', 'DewPointF': '13', 'FeelsLikeC': '-7', 'FeelsLikeF': '20', 'HeatIndexC': '-4', 'HeatIndexF': '24', 'WindChillC': '-7', 'WindChillF': '20', 'WindGustKmph': '8', 'WindGustMiles': '5', 'chanceoffog': '0', 'chanceoffrost': '82', 'chanceofhightemp': '0', 'chanceofovercast': '87', 'chanceofrain': '0', 'chanceofremdry': '89', 'chanceofsnow': '0', 'chanceofsunshine': '10', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '76', 'diffRad': '0.0', 'humidity': '60', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1036', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-4', 'tempF': '24', 'time': '300', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNE', 'winddirDegree': '29', 'windspeedKmph': '5', 'windspeedMiles': '3'}, {'DewPointC': '-17', 'DewPointF': '2', 'FeelsLikeC': '-10', 'FeelsLikeF': '14', 'HeatIndexC': '-5', 'HeatIndexF': '22', 'WindChillC': '-10', 'WindChillF': '14', 'WindGustKmph': '16', 'WindGustMiles': '10', 'chanceoffog': '0', 'chanceoffrost': '81', 'chanceofhightemp': '0', 'chanceofovercast': '86', 'chanceofrain': '0', 'chanceofremdry': '82', 'chanceofsnow': '0', 'chanceofsunshine': '6', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '100', 'diffRad': '0.0', 'humidity': '40', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1037', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-5', 'tempF': '22', 'time': '600', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '122', 'weatherDesc': [{'value': 'Overcast '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNE', 'winddirDegree': '22', 'windspeedKmph': '11', 'windspeedMiles': '7'}, {'DewPointC': '-22', 'DewPointF': '-8', 'FeelsLikeC': '-13', 'FeelsLikeF': '9', 'HeatIndexC': '-7', 'HeatIndexF': '20', 'WindChillC': '-13', 'WindChillF': '9', 'WindGustKmph': '22', 'WindGustMiles': '13', 'chanceoffog': '0', 'chanceoffrost': '80', 'chanceofhightemp': '0', 'chanceofovercast': '45', 'chanceofrain': '0', 'chanceofremdry': '94', 'chanceofsnow': '0', 'chanceofsunshine': '73', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '47', 'diffRad': '36.8', 'humidity': '27', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1039', 'pressureInches': '31', 'shortRad': '80.4', 'tempC': '-7', 'tempF': '20', 'time': '900', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'N', 'winddirDegree': '11', 'windspeedKmph': '17', 'windspeedMiles': '10'}, {'DewPointC': '-24', 'DewPointF': '-11', 'FeelsLikeC': '-12', 'FeelsLikeF': '10', 'HeatIndexC': '-5', 'HeatIndexF': '23', 'WindChillC': '-12', 'WindChillF': '10', 'WindGustKmph': '24', 'WindGustMiles': '15', 'chanceoffog': '0', 'chanceoffrost': '80', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '92', 'chanceofsnow': '0', 'chanceofsunshine': '85', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '18', 'diffRad': '64.6', 'humidity': '21', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1039', 'pressureInches': '31', 'shortRad': '313.8', 'tempC': '-5', 'tempF': '23', 'time': '1200', 'uvIndex': '2', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Sunny'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '340', 'windspeedKmph': '21', 'windspeedMiles': '13'}, {'DewPointC': '-25', 'DewPointF': '-13', 'FeelsLikeC': '-11', 'FeelsLikeF': '12', 'HeatIndexC': '-4', 'HeatIndexF': '24', 'WindChillC': '-11', 'WindChillF': '12', 'WindGustKmph': '25', 'WindGustMiles': '16', 'chanceoffog': '0', 'chanceoffrost': '79', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '80', 'chanceofsnow': '0', 'chanceofsunshine': '92', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '7', 'diffRad': '72.6', 'humidity': '19', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1038', 'pressureInches': '31', 'shortRad': '393.0', 'tempC': '-4', 'tempF': '24', 'time': '1500', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Sunny'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NW', 'winddirDegree': '323', 'windspeedKmph': '21', 'windspeedMiles': '13'}, {'DewPointC': '-26', 'DewPointF': '-15', 'FeelsLikeC': '-12', 'FeelsLikeF': '10', 'HeatIndexC': '-5', 'HeatIndexF': '22', 'WindChillC': '-12', 'WindChillF': '10', 'WindGustKmph': '27', 'WindGustMiles': '17', 'chanceoffog': '0', 'chanceoffrost': '79', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '94', 'chanceofsnow': '0', 'chanceofsunshine': '92', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '11', 'diffRad': '45.2', 'humidity': '18', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1039', 'pressureInches': '31', 'shortRad': '171.5', 'tempC': '-5', 'tempF': '22', 'time': '1800', 'uvIndex': '0', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Clear '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '334', 'windspeedKmph': '20', 'windspeedMiles': '12'}, {'DewPointC': '-27', 'DewPointF': '-17', 'FeelsLikeC': '-13', 'FeelsLikeF': '8', 'HeatIndexC': '-7', 'HeatIndexF': '20', 'WindChillC': '-13', 'WindChillF': '8', 'WindGustKmph': '24', 'WindGustMiles': '15', 'chanceoffog': '0', 'chanceoffrost': '78', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '86', 'chanceofsnow': '0', 'chanceofsunshine': '90', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '12', 'diffRad': '15.1', 'humidity': '18', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1042', 'pressureInches': '31', 'shortRad': '57.2', 'tempC': '-7', 'tempF': '20', 'time': '2100', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Clear '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '346', 'windspeedKmph': '18', 'windspeedMiles': '11'}], 'maxtempC': '-4', 'maxtempF': '24', 'mintempC': '-8', 'mintempF': '18', 'sunHour': '9.9', 'totalSnow_cm': '0.0', 'uvIndex': '0'}, {'astronomy': [{'moon_illumination': '1', 'moon_phase': 'Waxing Crescent', 'moonrise': '08:31 AM', 'moonset': '06:45 PM', 'sunrise': '07:32 AM', 'sunset': '05:19 PM'}], 'avgtempC': '-8', 'avgtempF': '18', 'date': '2026-01-20', 'hourly': [{'DewPointC': '-27', 'DewPointF': '-17', 'FeelsLikeC': '-15', 'FeelsLikeF': '4', 'HeatIndexC': '-8', 'HeatIndexF': '17', 'WindChillC': '-15', 'WindChillF': '4', 'WindGustKmph': '26', 'WindGustMiles': '16', 'chanceoffog': '0', 'chanceoffrost': '78', 'chanceofhightemp': '0', 'chanceofovercast': '16', 'chanceofrain': '0', 'chanceofremdry': '90', 'chanceofsnow': '0', 'chanceofsunshine': '92', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '21', 'diffRad': '0.0', 'humidity': '19', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1043', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-8', 'tempF': '17', 'time': '0', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'N', 'winddirDegree': '350', 'windspeedKmph': '19', 'windspeedMiles': '12'}, {'DewPointC': '-28', 'DewPointF': '-18', 'FeelsLikeC': '-16', 'FeelsLikeF': '3', 'HeatIndexC': '-9', 'HeatIndexF': '16', 'WindChillC': '-16', 'WindChillF': '3', 'WindGustKmph': '23', 'WindGustMiles': '15', 'chanceoffog': '0', 'chanceoffrost': '77', 'chanceofhightemp': '0', 'chanceofovercast': '44', 'chanceofrain': '0', 'chanceofremdry': '90', 'chanceofsnow': '0', 'chanceofsunshine': '88', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '44', 'diffRad': '0.0', 'humidity': '20', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1043', 'pressureInches': '31', 'shortRad': '0.0', 'tempC': '-9', 'tempF': '16', 'time': '300', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '116', 'weatherDesc': [{'value': 'Partly Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'N', 'winddirDegree': '352', 'windspeedKmph': '18', 'windspeedMiles': '11'}, {'DewPointC': '-28', 'DewPointF': '-19', 'FeelsLikeC': '-17', 'FeelsLikeF': '2', 'HeatIndexC': '-10', 'HeatIndexF': '15', 'WindChillC': '-17', 'WindChillF': '2', 'WindGustKmph': '21', 'WindGustMiles': '13', 'chanceoffog': '0', 'chanceoffrost': '77', 'chanceofhightemp': '0', 'chanceofovercast': '50', 'chanceofrain': '0', 'chanceofremdry': '89', 'chanceofsnow': '0', 'chanceofsunshine': '63', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '65', 'diffRad': '0.2', 'humidity': '20', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1043', 'pressureInches': '31', 'shortRad': '0.5', 'tempC': '-10', 'tempF': '15', 'time': '600', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '122', 'weatherDesc': [{'value': 'Overcast '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'N', 'winddirDegree': '350', 'windspeedKmph': '16', 'windspeedMiles': '10'}, {'DewPointC': '-29', 'DewPointF': '-19', 'FeelsLikeC': '-16', 'FeelsLikeF': '3', 'HeatIndexC': '-9', 'HeatIndexF': '15', 'WindChillC': '-16', 'WindChillF': '3', 'WindGustKmph': '20', 'WindGustMiles': '13', 'chanceoffog': '0', 'chanceoffrost': '76', 'chanceofhightemp': '0', 'chanceofovercast': '82', 'chanceofrain': '0', 'chanceofremdry': '86', 'chanceofsnow': '0', 'chanceofsunshine': '16', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '88', 'diffRad': '43.7', 'humidity': '19', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1043', 'pressureInches': '31', 'shortRad': '113.2', 'tempC': '-9', 'tempF': '15', 'time': '900', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '347', 'windspeedKmph': '15', 'windspeedMiles': '10'}, {'DewPointC': '-29', 'DewPointF': '-20', 'FeelsLikeC': '-14', 'FeelsLikeF': '7', 'HeatIndexC': '-7', 'HeatIndexF': '19', 'WindChillC': '-14', 'WindChillF': '7', 'WindGustKmph': '21', 'WindGustMiles': '13', 'chanceoffog': '0', 'chanceoffrost': '76', 'chanceofhightemp': '0', 'chanceofovercast': '57', 'chanceofrain': '0', 'chanceofremdry': '85', 'chanceofsnow': '0', 'chanceofsunshine': '36', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '44', 'diffRad': '85.2', 'humidity': '16', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1042', 'pressureInches': '31', 'shortRad': '297.8', 'tempC': '-7', 'tempF': '19', 'time': '1200', 'uvIndex': '2', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Sunny'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '338', 'windspeedKmph': '18', 'windspeedMiles': '11'}, {'DewPointC': '-29', 'DewPointF': '-20', 'FeelsLikeC': '-12', 'FeelsLikeF': '10', 'HeatIndexC': '-6', 'HeatIndexF': '22', 'WindChillC': '-12', 'WindChillF': '10', 'WindGustKmph': '22', 'WindGustMiles': '14', 'chanceoffog': '0', 'chanceoffrost': '67', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '90', 'chanceofsnow': '0', 'chanceofsunshine': '88', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '0', 'diffRad': '67.4', 'humidity': '14', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1040', 'pressureInches': '31', 'shortRad': '312.4', 'tempC': '-6', 'tempF': '22', 'time': '1500', 'uvIndex': '2', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Sunny'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NNW', 'winddirDegree': '328', 'windspeedKmph': '18', 'windspeedMiles': '11'}, {'DewPointC': '-28', 'DewPointF': '-18', 'FeelsLikeC': '-12', 'FeelsLikeF': '11', 'HeatIndexC': '-6', 'HeatIndexF': '22', 'WindChillC': '-12', 'WindChillF': '11', 'WindGustKmph': '22', 'WindGustMiles': '14', 'chanceoffog': '0', 'chanceoffrost': '50', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '86', 'chanceofsnow': '0', 'chanceofsunshine': '87', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '0', 'diffRad': '38.5', 'humidity': '16', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1039', 'pressureInches': '31', 'shortRad': '191.1', 'tempC': '-6', 'tempF': '22', 'time': '1800', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Clear '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NW', 'winddirDegree': '319', 'windspeedKmph': '15', 'windspeedMiles': '9'}, {'DewPointC': '-27', 'DewPointF': '-17', 'FeelsLikeC': '-12', 'FeelsLikeF': '10', 'HeatIndexC': '-7', 'HeatIndexF': '20', 'WindChillC': '-12', 'WindChillF': '10', 'WindGustKmph': '20', 'WindGustMiles': '13', 'chanceoffog': '0', 'chanceoffrost': '50', 'chanceofhightemp': '0', 'chanceofovercast': '0', 'chanceofrain': '0', 'chanceofremdry': '87', 'chanceofsnow': '0', 'chanceofsunshine': '90', 'chanceofthunder': '0', 'chanceofwindy': '0', 'cloudcover': '0', 'diffRad': '12.9', 'humidity': '18', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1039', 'pressureInches': '31', 'shortRad': '63.8', 'tempC': '-7', 'tempF': '20', 'time': '2100', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '113', 'weatherDesc': [{'value': 'Clear '}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'NW', 'winddirDegree': '318', 'windspeedKmph': '13', 'windspeedMiles': '8'}], 'maxtempC': '-6', 'maxtempF': '22', 'mintempC': '-10', 'mintempF': '14', 'sunHour': '8.3', 'totalSnow_cm': '0.0', 'uvIndex': '2'}]}
使用python自带的pprint(Pretty Print)库,结构就会十分的清晰:
python
{'current_condition': [{'FeelsLikeC': '-5',
'FeelsLikeF': '24',
'cloudcover': '0',
'humidity': '54',
'localObsDateTime': '2026-01-18 02:36 PM',
'observation_time': '06:36 AM',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1029',
'pressureInches': '30',
'temp_C': '-3',
'temp_F': '27',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly cloudy'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'SSE',
'winddirDegree': '163',
'windspeedKmph': '5',
'windspeedMiles': '3'}],
'nearest_area': [{'areaName': [{'value': 'Beijing'}],
'country': [{'value': 'China'}],
'latitude': '39.929',
'longitude': '116.388',
'population': '7480601',
'region': [{'value': 'Beijing'}],
'weatherUrl': [{'value': ''}]}],
'request': [{'query': 'Lat 39.91 and Lon 116.39', 'type': 'LatLon'}],
'weather': [{'astronomy': [{'moon_illumination': '1',
'moon_phase': 'New Moon',
'moonrise': '07:24 AM',
'moonset': '04:31 PM',
'sunrise': '07:33 AM',
'sunset': '05:17 PM'}],
'avgtempC': '-5',
'avgtempF': '22',
'date': '2026-01-18',
'hourly': [{'DewPointC': '-12',
'DewPointF': '11',
'FeelsLikeC': '-11',
'FeelsLikeF': '13',
'HeatIndexC': '-7',
'HeatIndexF': '20',
'WindChillC': '-11',
'WindChillF': '13',
'WindGustKmph': '11',
'WindGustMiles': '7',
'chanceoffog': '0',
'chanceoffrost': '86',
'chanceofhightemp': '0',
'chanceofovercast': '90',
'chanceofrain': '100',
'chanceofremdry': '0',
'chanceofsnow': '63',
'chanceofsunshine': '0',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '100',
'diffRad': '0.0',
'humidity': '68',
'precipInches': '0.0',
'precipMM': '0.1',
'pressure': '1030',
'pressureInches': '30',
'shortRad': '0.0',
'tempC': '-7',
'tempF': '20',
'time': '0',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '326',
'weatherDesc': [{'value': 'Light snow'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'E',
'winddirDegree': '97',
'windspeedKmph': '9',
'windspeedMiles': '5'},
{'DewPointC': '-10',
'DewPointF': '14',
'FeelsLikeC': '-11',
'FeelsLikeF': '12',
'HeatIndexC': '-7',
'HeatIndexF': '19',
'WindChillC': '-11',
'WindChillF': '12',
'WindGustKmph': '9',
'WindGustMiles': '6',
'chanceoffog': '0',
'chanceoffrost': '86',
'chanceofhightemp': '0',
'chanceofovercast': '93',
'chanceofrain': '100',
'chanceofremdry': '0',
'chanceofsnow': '72',
'chanceofsunshine': '0',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '100',
'diffRad': '0.0',
'humidity': '80',
'precipInches': '0.0',
'precipMM': '0.1',
'pressure': '1031',
'pressureInches': '30',
'shortRad': '0.0',
'tempC': '-7',
'tempF': '19',
'time': '300',
'uvIndex': '0',
'visibility': '5',
'visibilityMiles': '3',
'weatherCode': '332',
'weatherDesc': [{'value': 'Moderate snow'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'E',
'winddirDegree': '88',
'windspeedKmph': '7',
'windspeedMiles': '4'},
{'DewPointC': '-10',
'DewPointF': '13',
'FeelsLikeC': '-11',
'FeelsLikeF': '12',
'HeatIndexC': '-8',
'HeatIndexF': '18',
'WindChillC': '-11',
'WindChillF': '12',
'WindGustKmph': '9',
'WindGustMiles': '5',
'chanceoffog': '0',
'chanceoffrost': '85',
'chanceofhightemp': '0',
'chanceofovercast': '90',
'chanceofrain': '100',
'chanceofremdry': '0',
'chanceofsnow': '66',
'chanceofsunshine': '0',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '100',
'diffRad': '0.0',
'humidity': '81',
'precipInches': '0.0',
'precipMM': '0.1',
'pressure': '1031',
'pressureInches': '30',
'shortRad': '0.0',
'tempC': '-8',
'tempF': '18',
'time': '600',
'uvIndex': '0',
'visibility': '5',
'visibilityMiles': '3',
'weatherCode': '332',
'weatherDesc': [{'value': 'Moderate snow'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'E',
'winddirDegree': '88',
'windspeedKmph': '6',
'windspeedMiles': '4'},
{'DewPointC': '-12',
'DewPointF': '10',
'FeelsLikeC': '-9',
'FeelsLikeF': '16',
'HeatIndexC': '-7',
'HeatIndexF': '19',
'WindChillC': '-9',
'WindChillF': '16',
'WindGustKmph': '5',
'WindGustMiles': '3',
'chanceoffog': '0',
'chanceoffrost': '85',
'chanceofhightemp': '0',
'chanceofovercast': '83',
'chanceofrain': '0',
'chanceofremdry': '0',
'chanceofsnow': '82',
'chanceofsunshine': '0',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '100',
'diffRad': '29.8',
'humidity': '68',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1033',
'pressureInches': '31',
'shortRad': '62.0',
'tempC': '-7',
'tempF': '19',
'time': '900',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '311',
'weatherDesc': [{'value': 'Light freezing rain'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'ESE',
'winddirDegree': '119',
'windspeedKmph': '4',
'windspeedMiles': '2'},
{'DewPointC': '-12',
'DewPointF': '10',
'FeelsLikeC': '-6',
'FeelsLikeF': '21',
'HeatIndexC': '-5',
'HeatIndexF': '24',
'WindChillC': '-6',
'WindChillF': '21',
'WindGustKmph': '4',
'WindGustMiles': '3',
'chanceoffog': '0',
'chanceoffrost': '84',
'chanceofhightemp': '0',
'chanceofovercast': '94',
'chanceofrain': '0',
'chanceofremdry': '85',
'chanceofsnow': '0',
'chanceofsunshine': '16',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '82',
'diffRad': '97.3',
'humidity': '54',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1032',
'pressureInches': '30',
'shortRad': '240.7',
'tempC': '-5',
'tempF': '24',
'time': '1200',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '119',
'weatherDesc': [{'value': 'Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'S',
'winddirDegree': '170',
'windspeedKmph': '4',
'windspeedMiles': '2'},
{'DewPointC': '-12',
'DewPointF': '10',
'FeelsLikeC': '-3',
'FeelsLikeF': '26',
'HeatIndexC': '-3',
'HeatIndexF': '26',
'WindChillC': '-3',
'WindChillF': '26',
'WindGustKmph': '3',
'WindGustMiles': '2',
'chanceoffog': '0',
'chanceoffrost': '82',
'chanceofhightemp': '0',
'chanceofovercast': '42',
'chanceofrain': '0',
'chanceofremdry': '94',
'chanceofsnow': '0',
'chanceofsunshine': '81',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '55',
'diffRad': '95.2',
'humidity': '49',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1031',
'pressureInches': '30',
'shortRad': '360.6',
'tempC': '-3',
'tempF': '26',
'time': '1500',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'WSW',
'winddirDegree': '242',
'windspeedKmph': '3',
'windspeedMiles': '2'},
{'DewPointC': '-12',
'DewPointF': '11',
'FeelsLikeC': '-4',
'FeelsLikeF': '26',
'HeatIndexC': '-4',
'HeatIndexF': '26',
'WindChillC': '-4',
'WindChillF': '26',
'WindGustKmph': '3',
'WindGustMiles': '2',
'chanceoffog': '0',
'chanceoffrost': '81',
'chanceofhightemp': '0',
'chanceofovercast': '82',
'chanceofrain': '0',
'chanceofremdry': '86',
'chanceofsnow': '0',
'chanceofsunshine': '17',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '84',
'diffRad': '51.5',
'humidity': '53',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1033',
'pressureInches': '30',
'shortRad': '156.1',
'tempC': '-4',
'tempF': '26',
'time': '1800',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '119',
'weatherDesc': [{'value': 'Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'SW',
'winddirDegree': '235',
'windspeedKmph': '2',
'windspeedMiles': '1'},
{'DewPointC': '-11',
'DewPointF': '12',
'FeelsLikeC': '-4',
'FeelsLikeF': '25',
'HeatIndexC': '-4',
'HeatIndexF': '25',
'WindChillC': '-4',
'WindChillF': '25',
'WindGustKmph': '3',
'WindGustMiles': '2',
'chanceoffog': '0',
'chanceoffrost': '81',
'chanceofhightemp': '0',
'chanceofovercast': '34',
'chanceofrain': '0',
'chanceofremdry': '92',
'chanceofsnow': '0',
'chanceofsunshine': '88',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '44',
'diffRad': '0.0',
'humidity': '58',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1034',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-4',
'tempF': '25',
'time': '2100',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'WSW',
'winddirDegree': '251',
'windspeedKmph': '2',
'windspeedMiles': '1'}],
'maxtempC': '-3',
'maxtempF': '26',
'mintempC': '-8',
'mintempF': '18',
'sunHour': '8.5',
'totalSnow_cm': '0.5',
'uvIndex': '0'},
{'astronomy': [{'moon_illumination': '0',
'moon_phase': 'Waxing Crescent',
'moonrise': '08:01 AM',
'moonset': '05:38 PM',
'sunrise': '07:33 AM',
'sunset': '05:18 PM'}],
'avgtempC': '-5',
'avgtempF': '22',
'date': '2026-01-19',
'hourly': [{'DewPointC': '-10',
'DewPointF': '14',
'FeelsLikeC': '-4',
'FeelsLikeF': '24',
'HeatIndexC': '-4',
'HeatIndexF': '24',
'WindChillC': '-4',
'WindChillF': '24',
'WindGustKmph': '2',
'WindGustMiles': '1',
'chanceoffog': '0',
'chanceoffrost': '82',
'chanceofhightemp': '0',
'chanceofovercast': '38',
'chanceofrain': '0',
'chanceofremdry': '89',
'chanceofsnow': '0',
'chanceofsunshine': '87',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '39',
'diffRad': '0.0',
'humidity': '63',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1034',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-4',
'tempF': '24',
'time': '0',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'WNW',
'winddirDegree': '282',
'windspeedKmph': '1',
'windspeedMiles': '1'},
{'DewPointC': '-11',
'DewPointF': '13',
'FeelsLikeC': '-7',
'FeelsLikeF': '20',
'HeatIndexC': '-4',
'HeatIndexF': '24',
'WindChillC': '-7',
'WindChillF': '20',
'WindGustKmph': '8',
'WindGustMiles': '5',
'chanceoffog': '0',
'chanceoffrost': '82',
'chanceofhightemp': '0',
'chanceofovercast': '87',
'chanceofrain': '0',
'chanceofremdry': '89',
'chanceofsnow': '0',
'chanceofsunshine': '10',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '76',
'diffRad': '0.0',
'humidity': '60',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1036',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-4',
'tempF': '24',
'time': '300',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '119',
'weatherDesc': [{'value': 'Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNE',
'winddirDegree': '29',
'windspeedKmph': '5',
'windspeedMiles': '3'},
{'DewPointC': '-17',
'DewPointF': '2',
'FeelsLikeC': '-10',
'FeelsLikeF': '14',
'HeatIndexC': '-5',
'HeatIndexF': '22',
'WindChillC': '-10',
'WindChillF': '14',
'WindGustKmph': '16',
'WindGustMiles': '10',
'chanceoffog': '0',
'chanceoffrost': '81',
'chanceofhightemp': '0',
'chanceofovercast': '86',
'chanceofrain': '0',
'chanceofremdry': '82',
'chanceofsnow': '0',
'chanceofsunshine': '6',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '100',
'diffRad': '0.0',
'humidity': '40',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1037',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-5',
'tempF': '22',
'time': '600',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '122',
'weatherDesc': [{'value': 'Overcast '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNE',
'winddirDegree': '22',
'windspeedKmph': '11',
'windspeedMiles': '7'},
{'DewPointC': '-22',
'DewPointF': '-8',
'FeelsLikeC': '-13',
'FeelsLikeF': '9',
'HeatIndexC': '-7',
'HeatIndexF': '20',
'WindChillC': '-13',
'WindChillF': '9',
'WindGustKmph': '22',
'WindGustMiles': '13',
'chanceoffog': '0',
'chanceoffrost': '80',
'chanceofhightemp': '0',
'chanceofovercast': '45',
'chanceofrain': '0',
'chanceofremdry': '94',
'chanceofsnow': '0',
'chanceofsunshine': '73',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '47',
'diffRad': '36.8',
'humidity': '27',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1039',
'pressureInches': '31',
'shortRad': '80.4',
'tempC': '-7',
'tempF': '20',
'time': '900',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'N',
'winddirDegree': '11',
'windspeedKmph': '17',
'windspeedMiles': '10'},
{'DewPointC': '-24',
'DewPointF': '-11',
'FeelsLikeC': '-12',
'FeelsLikeF': '10',
'HeatIndexC': '-5',
'HeatIndexF': '23',
'WindChillC': '-12',
'WindChillF': '10',
'WindGustKmph': '24',
'WindGustMiles': '15',
'chanceoffog': '0',
'chanceoffrost': '80',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '92',
'chanceofsnow': '0',
'chanceofsunshine': '85',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '18',
'diffRad': '64.6',
'humidity': '21',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1039',
'pressureInches': '31',
'shortRad': '313.8',
'tempC': '-5',
'tempF': '23',
'time': '1200',
'uvIndex': '2',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Sunny'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '340',
'windspeedKmph': '21',
'windspeedMiles': '13'},
{'DewPointC': '-25',
'DewPointF': '-13',
'FeelsLikeC': '-11',
'FeelsLikeF': '12',
'HeatIndexC': '-4',
'HeatIndexF': '24',
'WindChillC': '-11',
'WindChillF': '12',
'WindGustKmph': '25',
'WindGustMiles': '16',
'chanceoffog': '0',
'chanceoffrost': '79',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '80',
'chanceofsnow': '0',
'chanceofsunshine': '92',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '7',
'diffRad': '72.6',
'humidity': '19',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1038',
'pressureInches': '31',
'shortRad': '393.0',
'tempC': '-4',
'tempF': '24',
'time': '1500',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Sunny'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NW',
'winddirDegree': '323',
'windspeedKmph': '21',
'windspeedMiles': '13'},
{'DewPointC': '-26',
'DewPointF': '-15',
'FeelsLikeC': '-12',
'FeelsLikeF': '10',
'HeatIndexC': '-5',
'HeatIndexF': '22',
'WindChillC': '-12',
'WindChillF': '10',
'WindGustKmph': '27',
'WindGustMiles': '17',
'chanceoffog': '0',
'chanceoffrost': '79',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '94',
'chanceofsnow': '0',
'chanceofsunshine': '92',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '11',
'diffRad': '45.2',
'humidity': '18',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1039',
'pressureInches': '31',
'shortRad': '171.5',
'tempC': '-5',
'tempF': '22',
'time': '1800',
'uvIndex': '0',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Clear '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '334',
'windspeedKmph': '20',
'windspeedMiles': '12'},
{'DewPointC': '-27',
'DewPointF': '-17',
'FeelsLikeC': '-13',
'FeelsLikeF': '8',
'HeatIndexC': '-7',
'HeatIndexF': '20',
'WindChillC': '-13',
'WindChillF': '8',
'WindGustKmph': '24',
'WindGustMiles': '15',
'chanceoffog': '0',
'chanceoffrost': '78',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '86',
'chanceofsnow': '0',
'chanceofsunshine': '90',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '12',
'diffRad': '15.1',
'humidity': '18',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1042',
'pressureInches': '31',
'shortRad': '57.2',
'tempC': '-7',
'tempF': '20',
'time': '2100',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Clear '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '346',
'windspeedKmph': '18',
'windspeedMiles': '11'}],
'maxtempC': '-4',
'maxtempF': '24',
'mintempC': '-8',
'mintempF': '18',
'sunHour': '9.9',
'totalSnow_cm': '0.0',
'uvIndex': '0'},
{'astronomy': [{'moon_illumination': '1',
'moon_phase': 'Waxing Crescent',
'moonrise': '08:31 AM',
'moonset': '06:45 PM',
'sunrise': '07:32 AM',
'sunset': '05:19 PM'}],
'avgtempC': '-8',
'avgtempF': '18',
'date': '2026-01-20',
'hourly': [{'DewPointC': '-27',
'DewPointF': '-17',
'FeelsLikeC': '-15',
'FeelsLikeF': '4',
'HeatIndexC': '-8',
'HeatIndexF': '17',
'WindChillC': '-15',
'WindChillF': '4',
'WindGustKmph': '26',
'WindGustMiles': '16',
'chanceoffog': '0',
'chanceoffrost': '78',
'chanceofhightemp': '0',
'chanceofovercast': '16',
'chanceofrain': '0',
'chanceofremdry': '90',
'chanceofsnow': '0',
'chanceofsunshine': '92',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '21',
'diffRad': '0.0',
'humidity': '19',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1043',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-8',
'tempF': '17',
'time': '0',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'N',
'winddirDegree': '350',
'windspeedKmph': '19',
'windspeedMiles': '12'},
{'DewPointC': '-28',
'DewPointF': '-18',
'FeelsLikeC': '-16',
'FeelsLikeF': '3',
'HeatIndexC': '-9',
'HeatIndexF': '16',
'WindChillC': '-16',
'WindChillF': '3',
'WindGustKmph': '23',
'WindGustMiles': '15',
'chanceoffog': '0',
'chanceoffrost': '77',
'chanceofhightemp': '0',
'chanceofovercast': '44',
'chanceofrain': '0',
'chanceofremdry': '90',
'chanceofsnow': '0',
'chanceofsunshine': '88',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '44',
'diffRad': '0.0',
'humidity': '20',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1043',
'pressureInches': '31',
'shortRad': '0.0',
'tempC': '-9',
'tempF': '16',
'time': '300',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '116',
'weatherDesc': [{'value': 'Partly Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'N',
'winddirDegree': '352',
'windspeedKmph': '18',
'windspeedMiles': '11'},
{'DewPointC': '-28',
'DewPointF': '-19',
'FeelsLikeC': '-17',
'FeelsLikeF': '2',
'HeatIndexC': '-10',
'HeatIndexF': '15',
'WindChillC': '-17',
'WindChillF': '2',
'WindGustKmph': '21',
'WindGustMiles': '13',
'chanceoffog': '0',
'chanceoffrost': '77',
'chanceofhightemp': '0',
'chanceofovercast': '50',
'chanceofrain': '0',
'chanceofremdry': '89',
'chanceofsnow': '0',
'chanceofsunshine': '63',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '65',
'diffRad': '0.2',
'humidity': '20',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1043',
'pressureInches': '31',
'shortRad': '0.5',
'tempC': '-10',
'tempF': '15',
'time': '600',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '122',
'weatherDesc': [{'value': 'Overcast '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'N',
'winddirDegree': '350',
'windspeedKmph': '16',
'windspeedMiles': '10'},
{'DewPointC': '-29',
'DewPointF': '-19',
'FeelsLikeC': '-16',
'FeelsLikeF': '3',
'HeatIndexC': '-9',
'HeatIndexF': '15',
'WindChillC': '-16',
'WindChillF': '3',
'WindGustKmph': '20',
'WindGustMiles': '13',
'chanceoffog': '0',
'chanceoffrost': '76',
'chanceofhightemp': '0',
'chanceofovercast': '82',
'chanceofrain': '0',
'chanceofremdry': '86',
'chanceofsnow': '0',
'chanceofsunshine': '16',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '88',
'diffRad': '43.7',
'humidity': '19',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1043',
'pressureInches': '31',
'shortRad': '113.2',
'tempC': '-9',
'tempF': '15',
'time': '900',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '119',
'weatherDesc': [{'value': 'Cloudy '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '347',
'windspeedKmph': '15',
'windspeedMiles': '10'},
{'DewPointC': '-29',
'DewPointF': '-20',
'FeelsLikeC': '-14',
'FeelsLikeF': '7',
'HeatIndexC': '-7',
'HeatIndexF': '19',
'WindChillC': '-14',
'WindChillF': '7',
'WindGustKmph': '21',
'WindGustMiles': '13',
'chanceoffog': '0',
'chanceoffrost': '76',
'chanceofhightemp': '0',
'chanceofovercast': '57',
'chanceofrain': '0',
'chanceofremdry': '85',
'chanceofsnow': '0',
'chanceofsunshine': '36',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '44',
'diffRad': '85.2',
'humidity': '16',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1042',
'pressureInches': '31',
'shortRad': '297.8',
'tempC': '-7',
'tempF': '19',
'time': '1200',
'uvIndex': '2',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Sunny'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '338',
'windspeedKmph': '18',
'windspeedMiles': '11'},
{'DewPointC': '-29',
'DewPointF': '-20',
'FeelsLikeC': '-12',
'FeelsLikeF': '10',
'HeatIndexC': '-6',
'HeatIndexF': '22',
'WindChillC': '-12',
'WindChillF': '10',
'WindGustKmph': '22',
'WindGustMiles': '14',
'chanceoffog': '0',
'chanceoffrost': '67',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '90',
'chanceofsnow': '0',
'chanceofsunshine': '88',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '0',
'diffRad': '67.4',
'humidity': '14',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1040',
'pressureInches': '31',
'shortRad': '312.4',
'tempC': '-6',
'tempF': '22',
'time': '1500',
'uvIndex': '2',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Sunny'}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NNW',
'winddirDegree': '328',
'windspeedKmph': '18',
'windspeedMiles': '11'},
{'DewPointC': '-28',
'DewPointF': '-18',
'FeelsLikeC': '-12',
'FeelsLikeF': '11',
'HeatIndexC': '-6',
'HeatIndexF': '22',
'WindChillC': '-12',
'WindChillF': '11',
'WindGustKmph': '22',
'WindGustMiles': '14',
'chanceoffog': '0',
'chanceoffrost': '50',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '86',
'chanceofsnow': '0',
'chanceofsunshine': '87',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '0',
'diffRad': '38.5',
'humidity': '16',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1039',
'pressureInches': '31',
'shortRad': '191.1',
'tempC': '-6',
'tempF': '22',
'time': '1800',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Clear '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NW',
'winddirDegree': '319',
'windspeedKmph': '15',
'windspeedMiles': '9'},
{'DewPointC': '-27',
'DewPointF': '-17',
'FeelsLikeC': '-12',
'FeelsLikeF': '10',
'HeatIndexC': '-7',
'HeatIndexF': '20',
'WindChillC': '-12',
'WindChillF': '10',
'WindGustKmph': '20',
'WindGustMiles': '13',
'chanceoffog': '0',
'chanceoffrost': '50',
'chanceofhightemp': '0',
'chanceofovercast': '0',
'chanceofrain': '0',
'chanceofremdry': '87',
'chanceofsnow': '0',
'chanceofsunshine': '90',
'chanceofthunder': '0',
'chanceofwindy': '0',
'cloudcover': '0',
'diffRad': '12.9',
'humidity': '18',
'precipInches': '0.0',
'precipMM': '0.0',
'pressure': '1039',
'pressureInches': '31',
'shortRad': '63.8',
'tempC': '-7',
'tempF': '20',
'time': '2100',
'uvIndex': '1',
'visibility': '10',
'visibilityMiles': '6',
'weatherCode': '113',
'weatherDesc': [{'value': 'Clear '}],
'weatherIconUrl': [{'value': ''}],
'winddir16Point': 'NW',
'winddirDegree': '318',
'windspeedKmph': '13',
'windspeedMiles': '8'}],
'maxtempC': '-6',
'maxtempF': '22',
'mintempC': '-10',
'mintempF': '14',
'sunHour': '8.3',
'totalSnow_cm': '0.0',
'uvIndex': '2'}]}
方法二:最正规法------看官方文档(API Docs)
这是最推荐的方法,也是大公司(eg:OpenAI, 高德地图,微信支付)的做法。
他们会提供一份详细的说明书,直接告诉你:"我的JSON长什么样,每个字段是什么意思。"
作为工程师的习惯:拿到一个API,第一反应先找文档,找不到文档(比如爬虫逆向时),再用其他方法。
方法三:浏览器F12(开发者工具)
如果你在爬取一个网页的数据(没有文档),或者你想快速看一眼某个接口返回了什么。
OPERATION STEPS:
- 打开浏览器(Chrome/Edge)
- 按F12打开开发者工具
- 点击顶部的"Network"(网络)标签
- 刷新一下网页
- 在过滤器里选择"Fetch/XHR"(这一步很关键,只看数据接口,不看图片/CSS)
- 在左侧列表里找到那个请求,点击它
- 点击右侧的"Preview"或"Response"
浏览器会把JSON自动折叠好,可以像点文件夹一样一层层点开。这比python打印出来看还要直观!
方法四: AI辅助法
只需要把那坨乱糟糟的JSON数据(用方法一打印出来),直接丢给AI
Prompt 示例:
"这是我从 API 拿到的 JSON 数据:[粘贴数据] 我想用 Python 提取出里面的'当前气温'和'天气描述',请帮我写出解析代码。"
AI直接给你写好
python
# 解析JSON数据
weather_data = {'current_condition': [{'FeelsLikeC': '-5', 'FeelsLikeF': '23', 'cloudcover': '0', 'humidity': '58', 'localObsDateTime': '2026-01-18 01:30 PM', 'observation_time': '05:30 AM', 'precipInches': '0.0', 'precipMM': '0.0', 'pressure': '1029', 'pressureInches': '30', 'temp_C': '-4', 'temp_F': '25', 'uvIndex': '1', 'visibility': '10', 'visibilityMiles': '6', 'weatherCode': '119', 'weatherDesc': [{'value': 'Cloudy'}], 'weatherIconUrl': [{'value': ''}], 'winddir16Point': 'S', 'winddirDegree': '180', 'windspeedKmph': '4', 'windspeedMiles': '2'}], 'nearest_area': [{'areaName': [{'value': 'Beijing'}], 'country': [{'value': 'China'}], 'latitude': '39.929', 'longitude': '116.388', 'population': '7480601', 'region': [{'value': 'Beijing'}], 'weatherUrl': [{'value': ''}]}], 'request': [{'query': 'Lat 39.91 and Lon 116.39', 'type': 'LatLon'}], 'weather': [{...}]}
# 提取当前气温和天气描述
temp = weather_data['current_condition'][0]['temp_C'] # 当前气温 = "-4"
desc = weather_data['current_condition'][0]['weatherDesc'][0]['value'] # 天气描述 = "Cloudy"
1.3.4 要点分析------requests.exceptions.RequestException
requests = 大工具箱 (Package/Folder) .exceptions = 工具箱里的 “报错说明书”分区 (Module/File) .RequestException = 说明书里的 第一条条款 (Class)"文件夹 -> 文件 -> 类".
1.3.5 要点分析------(keyError, IndexError)
出现原因
python
# ... 之前已经拿到了 data 字典 ...
# ⚠️ 风险代码 A
current_condition = data['current_condition'][0]
# ⚠️ 风险代码 B
weather_desc = current_condition['weatherDesc'][0]['value']
2. KeyError (键错误) ------ "查无此词"
KeyError
python
'''
来源: 字典 (dict) 操作。
含义: 你想在字典里找一个 Key(键),但是字典里根本没有这个 Key。
在你的代码中是如何发生的?
你的代码写着:data['current_condition']。
这意味着你坚信 API 返回的 data 字典里一定有个叫 'current_condition' 的东西。
实际情况: 如果你输错了城市名(比如 "NoCity"),wttr.in 可能会返回这样一个报错用的 JSON:
'''
{
"error_message": "Unable to find any matching weather location."
}
# 结果: Python 拿着这个字典去找 'current_condition',发现没有!于是当场抛出 KeyError。
IndexError
python
# 来源: 列表 (list) 操作。
'''
含义: 你想取列表里的第 N 个元素(比如第 0 个),但列表里的元素数量不够,或者列表根本是空的。
在你的代码中是如何发生的?
你的代码写着:['current_condition'][0] 或 ['weatherDesc'][0]。
那个 [0] 就是在取"第一个元素"。
实际情况: 假设 API 抽风了,返回了这样一个结构:
'''
{
"current_condition": [] # 注意!这里是个空列表
}
# 结果: 代码试图从空列表里拿第 1 个元素 ([0]),但箱子是空的。Python 就会抛出 IndexError: list index out of range。
# 比喻: 就像一本书只有 10 页,你却非要翻到第 11 页,或者一本书全是白纸你非要读第一行的字。
python
# 为什么要把它们放在一起?
except (KeyError, IndexError) as e:
# 因为对于你(开发者)来说,无论是"找不到键"还是"列表为空",后果是一样的:
# 结论: 数据格式不对,我没法提取天气信息。
# 处理方式: 统统归类为 "数据解析失败"。