-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
30 lines (27 loc) · 1014 Bytes
/
main.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
import requests
import BeautifulSoup
import collections
html_doc = requests.get("http://schedules.sofiatraffic.bg")
soup = BeautifulSoup.BeautifulSoup(html_doc.text.encode('ISO-8859-1'))
transports=[]
for link in soup.findAll('a'):
if 'tramway' in link.get('href') or 'autobus' in link.get('href') or 'trolleybus' in link.get('href'):
transports.append(link.get('href'))
import pprint
pprint.pprint(transports)
karta = collections.defaultdict(lambda: [])
for transport in transports:
url = "http://schedules.sofiatraffic.bg/"+transport
print url
html_doc = requests.get(url)
soup = BeautifulSoup.BeautifulSoup(html_doc.text.encode('ISO-8859-1'))
routes = soup.findAll('ul', { "class" : "schedule_direction_signs" })[:2]
for route in routes:
last_stop = None
stops = route.findAll('li')
for stop in stops:
current_stop = stop['class']
current_stop = current_stop[current_stop.find("stop_")+5:]
if last_stop:
karta[last_stop].append(current_stop)
last_stop = current_stop