Request - Getting data from the web

An API is an Application Programming interface. It is basically a gateway through which one program can access the data and services that a host organization is making available. It can be used for all sorts of things from weather, phone directories, google searches, contacts, facebook posts, twitter feeds etc etc.

There is no point giving you an comprehensive guide as every organization designs their API’s differently. Most work on requiring you to sign up to obtain an API Key, a code that you send with your request to authorise access to their data. This is done by hosts to help guard against their systems being overloaded. If you have the option, choose to access data in JSON format. That is what this example is based on, and it is the most common these days. If you can’t find an API for what you are looking for, you may want to consider using a web scraper such as Beautiful Soup (see related article).

Demo exercise: Using the Swiss public transport API

Look up the Swiss public transport timetable for the next 10 departures from the stop at Clochatte.


import requests
import json
from datetime import datetime

# API settings
api_key = ""

# Function to perform the API request
def get_station_info(station_name, limit=10):
    url = "https://timetable.search.ch/api/stationboard.json"
    params = {
        "stop": station_name,
        "limit":"10"
        }
    headers = {
        'Content-Type': 'application/json',
        'Authorization': '{0}'.format(api_key)
        }
    response = requests.get(url, headers=headers, params=params)
    if response.status_code == 200:
        data = json.loads(response.content.decode("utf-8"))
        # Print the forthcoming connections
        connections = data['connections']
        if isinstance(connections, list):
            messages = []
            for c in connections:
                departure_time = datetime.strptime(c["time"], "%Y-%m-%d %H:%M:%S")
                time_remaining = departure_time - datetime.now()
                minutes = int(time_remaining.total_seconds() / 60)
                destination = c["terminal"]["name"]
                messages.append("{0} {1} departs in {2} minutes bound for {3}".format(
                    c["type"], c["line"], minutes, destination))
            return(messages)
        else:
            return("Something has gone wrong :-(")
    else:
        return None

# Get the data
station_name = "Lausanne, Clochatte"
data = get_station_info(station_name)

# Print station name
print("Next departures for {0}".format(station_name))
for item in data:
    print(item)

Public API’s

Some that look quite interesting