Python3:从XML中提取指定站点的UV-index值
提取指定站点的UV指数值
以下是几种从目标XML文件中提取Roomassaare站点UV指数的实用方法:
Python 脚本实现
使用Python内置的XML解析库直接获取数据:
import xml.etree.ElementTree as ET import urllib.request # 获取并解析XML数据 with urllib.request.urlopen("https://www.ilmateenistus.ee/ilma_andmed/xml/observations.php") as response: root = ET.fromstring(response.read()) # 定位目标站点并提取UV指数 for station in root.findall("station"): station_name = station.find("name").text if station_name == "Roomassaare": uv_index = station.find("uvindex").text print(f"Roomassaare的UV指数:{uv_index}") break
命令行工具实现
方法1:curl + xmllint
适用于Linux/macOS系统(需安装xmllint):
curl -s "https://www.ilmateenistus.ee/ilma_andmed/xml/observations.php" | xmllint --xpath '//station[name="Roomassaare"]/uvindex/text()' -
方法2:curl + xq(yq工具集)
需先安装xq(属于yq工具集):
curl -s "https://www.ilmateenistus.ee/ilma_andmed/xml/observations.php" | xq '.observations.station[] | select(.name == "Roomassaare") | .uvindex'
内容的提问来源于stack exchange,提问作者fred
相关产品推荐
相关产品推荐

