A Python library to query Deutsche Bahn journey, departure, and arrival data using the (unofficial) API.
- 📦 Installation
- 🛠️ Usage
- 🔧 Initialize PyBahn
- 🔍 Search Stations
- 📍 Get a Single Station
- 🗺️ Nearby Stations
- 🚉 Departures
- 🚉 Arrivals
- 🚆 Journeys
- 🎯 Filter Departures/Arrivals
- 🎯 Filter Journeys
- 🚆 Stopovers
⚠️ License⚠️ Disclaimer
pip install pybahnfrom pybahn import PyBahn
bahn = PyBahn()stations = bahn.stations("Berlin")
for station in stations:
print(station.name)station = bahn.station("Munich")
print(station.name)near_stations = bahn.nearby(latitude=52.5170365, longitude=13.3888599, limit=5)
for station in stations:
print(station.name)munich = bahn.station("Munich")
departures = bahn.departures(munich) # Berlin Hbf
print(departures[0].lineName)station = bahn.station("Munich")
arrivals = bahn.arrivals(station)
print(arrivals[0].lineName)station1 = bahn.station("Frankfurt")
station2 = bahn.station("Berlin")
journeys = bahn.journeys(station1, station2)
print(journeys[0].arrival_name)from pybahn.structs import Filter, Date
berlin = bahn.station("Berlin hbf")
frankfurt = bahn.station("Frankfurt")
date = Date().set_time(hour=12, minute=00).set_date(month=10, day=2)
filters = Filter.from_list(["S", "U", "BUS"])
berlin_departures = bahn.departures(berlin, filters=filters, date=date)
print(berlin_departures[0].line_name)
berlin_arrivals = bahn.arrivals(berlin, filters=filters, date=date)
print(berlin_arrivals[0].line_name)from pybahn.structs import Products
station1 = bahn.station("Frankfurt")
station2 = bahn.station("Berlin")
journeys = bahn.journeys(station1, station2, products=Products.REGIONALS)
print(journeys[0].changes_amont)from pybahn.structs import Products
station1 = bahn.station("Frankfurt")
station2 = bahn.station("Berlin")
journeys = bahn.journeys(
departure=station2,
destination=station1,
products=[Products.EC_IC, Products.REGIONAL]
)
print(journeys[0].changes_amont)from pybahn.structs import Date
station1 = bahn.station("Kassel")
station2 = bahn.station("Berlin")
stopover = bahn.station("frankfurt")
stopover.stopover_time = 5
journeys = bahn.journeys(departure=station1, destination=station2, stopovers=[stopo])
print(journeys)This library (pybahn) is an unofficial wrapper around Deutsche Bahn’s internal web APIs, discovered via publicly accessible browser traffic.
- It is not affiliated with or endorsed by Deutsche Bahn.
- It uses undocumented and unsupported endpoints, which may break or change without notice.
- Use this library at your own risk.
- Please respect Deutsche Bahn’s terms of use and avoid abusive behavior.