Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: 0fd3f800ef
Fetching contributors…

Cannot retrieve contributors at this time

61 lines (50 sloc) 2.544 kb
#!/usr/bin/env python
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
from datetime import datetime, timedelta
class NationalRailBrowser(Browser):
BASE = ''
def search(self, _from, _to, via=None,,, directTrainsOnly=False, includeOvertakenTrains=False):
self['from.searchTerm'] = _from
self['to.searchTerm'] = _to
if via: self['via.searchTerm'] = via
outwardTime += timedelta(minutes = self._minute(outwardTime.minute)-outwardTime.minute)
self[''] = [outwardTime.strftime('%d')]
self['timeOfOutwardJourney.month'] = [outwardTime.strftime('%B')]
self['timeOfOutwardJourney.hour'] = [outwardTime.strftime('%H')]
self['timeOfOutwardJourney.minute'] = [outwardTime.strftime('%M')]
returnTime += timedelta(hours = 2)
returnTime += timedelta(minutes = self._minute(returnTime.minute)-returnTime.minute)
self[''] = [returnTime.strftime('%d')]
self['timeOfReturnJourney.month'] = [returnTime.strftime('%B')]
self['timeOfReturnJourney.hour'] = [returnTime.strftime('%H')]
self['timeOfReturnJourney.minute'] = [returnTime.strftime('%M')]
if directTrainsOnly: self['maxChanges'] = ['true']
if includeOvertakenTrains: self['includeOvertakenTrains'] = ['true']
r = self.submit()
b = BeautifulSoup(r)
table = b.find(attrs={'id':'ResultsTable'})
tbody = table.find('tbody') # the first of many
tr = tbody.findAll('tr')
departure = [td.contents[0] for td in tr[3].findAll('td')]
arrival = [td.contents[0] for td in tr[4].findAll('td')]
duration = [td.contents[0] for td in tr[6].findAll('td')]
changes = [td.contents[0] for td in tr[7].findAll('td')]
return zip(departure, arrival, duration, changes)
def _minute(self, m):
res,rem = divmod(m,15)
if rem == 0:
return m
return (res+1)*15
if __name__=='__main__':
import sys
if len(sys.argv) < 3:
print 'Usage: python from to\nExample: python SHF "Birmingham"'
_from, _to = sys.argv[1:3]
b = NationalRailBrowser()
for journey in, _to):
print 'departure: %s\narrival: %s\nduration: %s\nchanges: %s\n' % journey
Jump to Line
Something went wrong with that request. Please try again.