Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Background info
- '''
- 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,
- http://svc.metrotransit.org/NexTrip/17940?format=json
- (North from Hennepin and 16th)
- http://svc.metrotransit.org/NexTrip/17928?format=json
- (South from Hennepin and 16th)
- 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.
- '''
- #Program function
- import requests
- def fetch(url):
- response = requests.get(url).json()
- return response
- urls = ['http://svc.metrotransit.org/NexTrip/17940?format=json', 'http://svc.metrotransit.org/NexTrip/17928?format=json']
- counter = 0
- for url in urls:
- counter = counter + 1
- response = fetch(url)
- print(response)
- print(response[0]['Route'])
- print(response[0]['DepartureText'])
- print(response[0]['Description'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement