Snippets

Transport for London 4qxBMb: Untitled snippet

Created by Leon Byford
import requests
import xml.etree.ElementTree as ET

LINE_CODES = ['B', 'C', 'D', 'H', 'J', 'M', 'N', 'P', 'V', 'W']

print('line_code,station_code,station_name,status_code')
for line in LINE_CODES:
    res = requests.get(f'http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/{line}')
    res.encoding = 'utf-8-sig'
    tree = ET.fromstring(res.text)
    stations = tree.findall('./S')
    for station in stations:
        station_code = station.get('Code')
        station_name = station.get('N')
        res = requests.get(f'http://cloud.tfl.gov.uk/TrackerNet/PredictionDetailed/{line}/{station_code}')
        print(f'{line},{station_code},{station_name},{res.status_code}')

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.