python解析xm

2024-06-17 19:40:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本的脚本,可以解析温度,露点,高度计等。但是,我如何才能解析一个条件字符串,如天空条件?我想分析数据并打印出来:“天空条件:2000英尺AGL处很少”。在

import xml.etree.ElementTree as ET
from urllib import urlopen

link = urlopen('http://weather.aero/dataserver_current/httpparam?dataSource=metars&       requestType=retrieve&format=xml&stationString=KSFO&hoursBeforeNow=1')

tree = ET.parse(link)
root = tree.getroot()

data = root.findall('data/METAR')
for metar in data:
    print metar.find('temp_c').text

Tags: import脚本treedatalinkrootxml温度
1条回答
网友
1楼 · 发布于 2024-06-17 19:40:12

您正在检索的页面具有如下结构:

<METAR>
  <!  snip  >
  <sky_condition sky_cover="FEW" cloud_base_ft_agl="2000"/>
  <sky_condition sky_cover="BKN" cloud_base_ft_agl="18000"/>
</METAR>

所以您要问的是如何提取XML属性。The xml.etree.ElementTree docs声明这些存储在名为attrib的字典中。所以你的代码应该是这样的:

^{pr2}$

相关问题 更多 >