0

I am using this this tutorial to go through some concepts of Python, however, I ran into this problem trying to use this line of a code in 2.76. Dave is using 2.73 at the 53 minute part of the video in the monitor.py file.

I am using this line of code to try to fix an error...

from xml.etree.ElementTree import parse

As oppose to

import xml.etree.ElementTree import parse 

(which is what is used in the video in python 2.7.3)

however it doesn't work when I use it. I get this error instead.

   PS C:\python27> python monitor.py
   Traceback (most recent call last):
     File "monitor.py", line 28, in <module>
        monitor()
      File "monitor.py", line 15, in monitor
       doc = parse(u)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
       tree.parse(source, parser)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 656, in parse
       parser.feed(data)
     File "C:\Python27\lib\xml\etree\ElementTree.py", line 1642, in feed
        self._raiseerror(v)
      File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
        raise err
    xml.etree.ElementTree.ParseError: undefined entity &nbsp;: line 50, column 47

What import line of code should I write and why? I have tried 'as' and 'import'

Here is the code that I am using.

#monitor.py
import urllib
from xml.etree.ElementTree import parse

candidates = ['4131','4163','4132']
daves_latitude = 41.980262


def distance(lat1, lat2):
    'Return distance in miles between two lats'
    return 69*(lat1 - lat2)

def monitor():
    u = urllib.urlopen('http://ctabustracker.com/bustime/mapgetBusesForRoute.jsp?route=22')
    doc = parse(u)
    for bus in doc.findall('bus'):
        busid = bus.findtext('id')
        if busid in candidates:
            lat = float(bus.findtext('lat'))
            dis = distance(lat, daves_latitude)
            print busid, dis, 'miles'
        print '-'*10


import time

while True:
    monitor()
    time.sleep(60)
karthikr
  • 97,368
  • 26
  • 197
  • 188
Robert Birch
  • 251
  • 2
  • 4
  • 16

1 Answers1

0
#XXX INCORRECT, DO NOT USE IT
import xml.etree.ElementTree import parse 

The error xml.etree.ElementTree.ParseError: undefined entity &nbsp is not related to how you import ElementTree module in any way. The behaviour is the same in Python 2.7.6 and 2.7.3.

@karthikr linked to the question in the comments that explains the issue: &nbsp; is not defined for xml by default, see Parse XML with (X)HTML entities.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670