ElementTree - ParseError:格式不正确(令牌无效)

时间:2014-12-14 15:00:17

标签: python python-2.7 xml-parsing elementtree urlopen

我尝试使用urlopen打开XML文件并阅读它。但是,我一直收到以下错误:xml.etree.ElementTree.ParseError:格式不正确(无效令牌)

以下是代码:

    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

1 个答案:

答案 0 :(得分:0)

我想问题出在这一行tree = ET.fromstring(doc, parser=parser)。将其更改为

 tree = ET.fromstring(doc)

似乎ET.fromstring接受一个参数。