SHARE
TWEET

getroutes.py - SF Muni Google Map

asweigart Feb 10th, 2014 453 Never
  1. import requests
  2. import xml.etree.ElementTree as ET
  3.  
  4. # use the requests module to download the webpage (you can copy and paste this page into your browser to see it for yourself)
  5. req = requests.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=sf-muni')
  6.  
  7. tree = ET.fromstring(req.text) # parse the XML in the webpage
  8. root = tree.getroot() # get the first (root) XML element
  9. routes = {} # this dictionary will store all the routes names and their titles
  10. for child in root: # loop through all the routes
  11.     routes[child.attrib['tag']] = child.attrib['title'] # key is route name, value is full route name
  12.  
  13. # save the routes dictionary to a text file (routedata.py) to use later
  14. import pprint
  15. fo = open('routedata.py', 'w')
  16. fo.write('allRoutes = ' + pprint.pformat(routes))
  17. fo.close()
RAW Paste Data
Top