0

I'm trying to open an XML file using urlopen and reading it. However, I keep getting the following error: xml.etree.ElementTree.ParseError: not well-formed (invalid token)

Here is the code:

    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
Savvis
  • 53
  • 1
  • 3
  • 7

1 Answers1

0

I guess the problem lies in this line tree = ET.fromstring(doc, parser=parser). Change this to

 tree = ET.fromstring(doc)

It seems ET.fromstring accepts one parameter.

kvivek
  • 3,321
  • 1
  • 15
  • 17
  • So, I changed the following and got the same error: `code` link = urlopen('http://w1.weather.gov/xml/current_obs/%s.xml' % icao).read().decode('utf-8') tree = ET.fromstring(link) – Savvis Dec 14 '14 at 15:41
  • I just looked at the url "http://w1.weather.gov/xml/current_obs/" and with respect to the xml the icao variable string which you are generating should be proper. Try hardcoding it and check. The ICAO variable string should be on the strings present in this zip http://w1.weather.gov/xml/current_obs/all_xml.zip – kvivek Dec 14 '14 at 15:45