You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何从JSON列表提取所有火山名称?

提取所有火山名称的解决方案

你的问题出在只取了API返回列表的第一个元素(v[0]),要获取所有火山名称,只需要遍历整个返回的列表即可,下面是两种适合新手的实现方式:

方式1:列表推导式(简洁写法)

import requests

# 直接使用API地址,避免不必要的format()调用
url = "https://volcanoes.usgs.gov/hans2/apiv2/volcanoApi/allWithNotice"
volcano_list = requests.get(url).json()

# 一行代码提取所有火山名称
all_volcano_names = [item['volcName'] for item in volcano_list]

# 验证结果
for name in all_volcano_names:
    print(name)

方式2:普通for循环(更易理解)

import requests

url = "https://volcanoes.usgs.gov/hans2/apiv2/volcanoApi/allWithNotice"
volcano_list = requests.get(url).json()

all_volcano_names = []
# 遍历列表里的每一个火山对象
for volcano in volcano_list:
    # 将当前火山的名称添加到结果列表
    all_volcano_names.append(volcano['volcName'])

# 打印所有名称
print(all_volcano_names)

关键说明

  • API返回的volcano_list是一个包含多个字典的列表,每个字典对应一座火山的信息,v[0]只是列表的第一个元素,所以遍历整个列表才能拿到所有数据。
  • 如果之后需要提取火山的其他信息(比如经纬度、预警状态),只需要替换'volcName'为对应的键名即可(可以打印volcano_list[0]查看所有可用键)。

内容的提问来源于stack exchange,提问作者Harry BM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:30:57