Skip to content

Instantly share code, notes, and snippets.

@westphahl
Created May 24, 2011 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westphahl/988332 to your computer and use it in GitHub Desktop.
Save westphahl/988332 to your computer and use it in GitHub Desktop.
Ankünfte und Verspätungen für Bahnhof
#!/usr/bin/env python
import sys
import urllib2
from urllib2 import URLError
from BeautifulSoup import BeautifulSoup
URL = "http://reiseauskunft.bahn.de/bin/bhftafel.exe/dn?rt=1&input=%s&boardType=arr&time=actual&productsFilter=11110&start=yes"
if __name__ == '__main__':
bhf_name = "Berlin Hbf"
try:
response = urllib2.urlopen(URL % urllib2.quote(bhf_name))
except URLError:
print("urllib2.urlopen(): Error fetching url.\n")
sys.exit(1)
html = response.read()
soup = BeautifulSoup(html)
table = soup.find('table', 'result')
rows_raw = table.findAll('tr')
# Ueberschriften und Navigation entfernen
rows_raw = rows_raw[2:-1]
print("\n>>> Ankuenfte und Verspaetungen: %s\n" % bhf_name)
delay = True
for row in rows_raw:
time = row('td', 'time')[0].text
if (row.get('class') == 'current'):
delay = False
print("---------------------------")
print("Aktuelle Uhrzeit: %s Uhr" % time)
print("---------------------------")
continue
train = ' '.join(row('td', 'train')[1].text.split())
if delay: print("Verspaetung: %s @ %s Uhr" % (train, time))
else: print("Ankunft: %s @ %s Uhr" % (train, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment