-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoxscraper.py
62 lines (42 loc) · 1.24 KB
/
oxscraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
from BeautifulSoup import BeautifulSoup
def get_content(URL):
"""
Pass a URL. Get back some Beautiful Soup.
"""
r = requests.get(URL)
soup = BeautifulSoup(r.text)
return soup
def parse_soup(soup):
"""
parses the soup we get from get_buses
"""
stopTable = soup.findAll('table')[1]
stopBody = stopTable.find('tbody')
if not stopBody:
return []
rows = stopBody.findAll('tr')
bus_list = []
for row in rows:
d = {}
cells = row.findAll('td')
d['service'] = cells[0].string
d['destination'] = cells[1].string.replace('&', '&')
d['minutes_to_departure'] = cells[2].string.replace(' mins', '')
if d['minutes_to_departure'] == 'DUE':
d['minutes_to_departure'] = 0
bus_list.append(d)
return bus_list
class stop(object):
"""
init with a stop id
get back buses for that stop as a list of dictionaries
"""
def get_buses(self):
url = 'http://www.oxontime.com/Naptan.aspx' \
'?t=departure&sa=%s&format=xhtml' % (self.id)
soup = get_content(url)
self.bus_list = parse_soup(soup)
def __init__(self, id):
self.id = id
self.get_buses()