Created
October 15, 2014 10:01
-
-
Save davidgillen/193275a9c59d6cb6e38b to your computer and use it in GitHub Desktop.
Query dublin bus to get the times for a specific bus number at a certain stop. e.g. python busTimes.php 4 327
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import urllib2 | |
import datetime | |
from bs4 import BeautifulSoup | |
# Get our arguments | |
parser = argparse.ArgumentParser() | |
parser.add_argument("busNumber") | |
parser.add_argument("stopNumber") | |
args = parser.parse_args() | |
busNumber = args.busNumber | |
stopNumber = args.stopNumber | |
times = 'http://www.dublinbus.ie/en/RTPI/Sources-of-Real-Time-Information/?searchtype=view&searchquery=' + stopNumber | |
soup = BeautifulSoup(urllib2.urlopen(times).read()) | |
table = soup.find('table', attrs={'id': 'rtpi-results'}) | |
rows = table.find_all('tr', {'class': ['even','odd']}) | |
output = datetime.datetime.today().strftime('%H:%M:%S %d/%m/%Y,') | |
for row in rows: | |
cols = row.find_all('td') | |
if cols[0].find(text=True).strip() == busNumber: | |
output += cols[2].find(text=True).strip() + "," | |
print output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment