Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

getroutes.py - SF Muni Google Map

By: asweigart on Feb 10th, 2014  |  syntax: Python  |  size: 0.81 KB  |  views: 230  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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()