Skip to content

qwwesq/PyBahn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pybahn

A Python library to query Deutsche Bahn journey, departure, and arrival data using the (unofficial) API.

πŸ“š Table of Contents

πŸ“¦ Installation

pip install pybahn

πŸ› οΈ Usage

πŸ”§ Initialize PyBahn

from pybahn import PyBahn

bahn = PyBahn()

πŸ” Search Station

berlins = bahn.stations("Berlin")

print(berlins[0].name)

πŸ“ Get a Single Station

munich = bahn.station("Munich")

πŸ—ΊοΈ Nearby Stations

stations = bahn.nearby(latitude=00.00000, longitude=00.0000)

πŸš‰ Departures

departures = bahn.departures("8000105")  # Berlin Hbf

print(departures[0].lineName)

or

station = bahn.station("Munich")

departures = bahn.departures(station)

print(departures[0].lineName)

πŸš‰ Arrivals

arrivals = bahn.arrivals("8000105")  # Berlin Hbf

print(arrivals[0].lineName)

or

station = bahn.station("Munich")

arrivals = bahn.arrivals(station)

print(arrivals[0].lineName)

πŸš† Journeys

station1 = bahn.station("Frankfurt")

station2 = bahn.station("Berlin")

journeys = bahn.journeys(station1.lid, stat2.lid)

print(journeys[0].arrival_name)

or

station1 = bahn.station("Frankfurt")

station2 = bahn.station("Berlin")

journeys = bahn.journeys(station1, station2)

print(journeys[0].arrival_name)

🎯 Filter Departures/Arrivals

from pybahn.structs import Products
from datetime import datetime

station1 = bahn.station("Frankfurt")

station2 = bahn.station("Berlin")

time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")

journeys = bahn.journeys(departure = station1, 
                      destination = station2, 
                      time = time,
                      products = [Products.EC_IC, Products.SBAHN],
                      only_d_ticket = True)

print(journeys[0].changes_amont)

🎯 Filter Journeys

from pybahn.structs import Products


station1 = bahn.station("Frankfurt")

station2 = bahn.station("Berlin")

journeys = bahn.journeys(station1.lid, station2.lid, products=Products.REGIONALS)

print(journeys[0].changes_amont)

or

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)

πŸš† Stopovers

from datetime import datetime


station1 = bahn.station("kassel")

station2 = bahn.station("Berlin")

stopo = bahn.station("frankfurt")
stopo.stopover_time = 5


time = datetime.strptime("2025-05-23T21:40:00", "%Y-%m-%dT%H:%M:%S")
time = time.strftime("%Y-%m-%dT%H:%M:%S")


journeys = bahn.journeys(departure=station1, destination=station2, stopovers=[stopo], time=time)


print(journeys)

πŸ“„ License

MIT License

⚠️ Disclaimer

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.

About

A Python library for interacting with Deutsche Bahn data. Please use a PyPi for get the latest version :(

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages