Advertisement
RiseVisual

Untitled

Apr 16th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #Background info
  2. '''
  3. Metro Transit has an API with live bus information. These URLs return upcoming bus departures for the two bus stops outside Minneapolis College on Hennepin Avenue at 16th Street,
  4.  
  5. http://svc.metrotransit.org/NexTrip/17940?format=json
  6.  
  7. (North from Hennepin and 16th)
  8.  
  9. http://svc.metrotransit.org/NexTrip/17928?format=json
  10.  
  11. (South from Hennepin and 16th)
  12.  
  13. For this program, make two queries to each of these APIs. Process the responses and print two tables, one for each stop. Each table should have the bus number, route, and arrival time, for the next buses at both of these stops. You will need to get the Route, the DepartureText, (a human-readable time) and Description (the bus route) from the response, for each bus.
  14. '''
  15.  
  16. #Program function
  17. import requests
  18.  
  19. def fetch(url):
  20. response = requests.get(url).json()
  21. return response
  22.  
  23. urls = ['http://svc.metrotransit.org/NexTrip/17940?format=json', 'http://svc.metrotransit.org/NexTrip/17928?format=json']
  24.  
  25. counter = 0
  26.  
  27. for url in urls:
  28. counter = counter + 1
  29. response = fetch(url)
  30. print(response)
  31. print(response[0]['Route'])
  32. print(response[0]['DepartureText'])
  33. print(response[0]['Description'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement