【发布时间】:2014-12-14 15:00:17
【问题描述】:
我正在尝试使用 urlopen 打开一个 XML 文件并读取它。但是,我不断收到以下错误:xml.etree.ElementTree.ParseError: not well-formed (invalid token)
代码如下:
def wx(icao):
if re.search(r'!wx *\w', icao):
icao = ircmsg.split('PRIVMSG')[-1].split(':!wx')[1]
icao = icao.upper()
link = urlopen('http://w1.weather.gov/xml/current_obs/%s.xml' % icao)
doc = link.read()
parser = ET.XMLParser(encoding='utf-8')
tree = ET.fromstring(doc, parser=parser)
station_id = tree.find('station_id').text
location = tree.find('location').text
observation_time = tree.find('observation_time_rfc822').text
wind = tree.find('wind_string').text
vis = tree.find('visibility_mi').text
weather = tree.find('weather').text
temp = tree.find('temperature_string').text
dew = tree.find('dewpoint_string').text
【问题讨论】:
-
对我有用,也许您要求的国际民航组织代码错误。
-
我使用的是 Python 2.7.9 并且无法正常工作。我仔细检查了国际民航组织,它是有效的。
-
在我的测试中,我删除了
re和ircmsg行,所以问题可能出在这两个上。
标签: python python-2.7 xml-parsing elementtree urlopen