python script from youtube video doesn't work
Question: I am trying to learn python from this youtube video:
<https://www.youtube.com/watch?v=RrPZza_vZ3w>

In the video they have given the viewers a script to run:

    
    
    ">>> import urllib"
    
    ">>> u = urllib.urlopen('http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')"
    
    ">>> data = u.read()"
    
    ">>> f = open('rt22.xml','wb')"
    
    ">>> f.write(data)"
    
    ">>> f.close()"
    

which pulls up data from a website and saves it in an xml file. But when I
check the xml file I only get this: XML Parsing Error: no element found


Answer: I am pretty sure this is all the code needed for the video, It might help as a
reference.

    
    
    import urllib
    import webbrowser
    import time
    from xml.etree.ElementTree import parse
    
    u = urllib.urlopen("http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22")
    data = u.read()
    
    with open("rt22.xml", "wb") as f:
        f.write(data)
        f.close()
    
    office_lat = 41.980262
    doc = parse("rt22.xml")
    
    
    def distance(lat1, lat2):
        'Return approx miles between lat1 and lat2'
        return 69 * abs(lat1 - lat2)
    
    
    def check_bus_location():
        for bus in doc.findall("bus"):
            if bus.findtext("lat") >= office_lat:
                latitude = float(bus.findtext("lat"))
                longitude = float(bus.findtext("lon"))
                bus_id = (bus.findtext("id"))
                direction = bus.findtext("d")
                north_buses = [[bus_id, latitude, longitude]]
                if direction.startswith("North"):
                    print('%s %s %0.2f miles' % (bus_id, direction, distance(latitude, office_lat)))
                    for bus in north_buses:
                        if distance(float(latitude), office_lat) < 0.5:
                            print(webbrowser.open(
                                'http://maps.googleapis.com/maps/api/staticmap?size=500x500&sensor=false&markers=|%f,%f' % (
                                    latitude, longitude)))
    
    
    while True:
        check_bus_location()
        time.sleep(10)
    

