我有一个基本的脚本,可以解析温度,露点,高度计等。但是,我如何才能解析一个条件字符串,如天空条件?我想分析数据并打印出来:“天空条件: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
您正在检索的页面具有如下结构:
所以您要问的是如何提取XML属性。The xml.etree.ElementTree docs声明这些存储在名为
^{pr2}$attrib
的字典中。所以你的代码应该是这样的:相关问题 更多 >
编程相关推荐