Repository navigation
Expand file tree
/
Copy pathscrape.py
More file actions
61 lines (47 loc) · 1.83 KB
/
Copy pathscrape.py
File metadata and controls
61 lines (47 loc) · 1.83 KB
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
import requests
from datetime import datetime
def remaining_minutes(bus_time, now):
h = int(bus_time.split(':')[0])
m = int(bus_time.split(':')[1])
arrival = now.replace(hour=h, minute=m)
remaining = arrival-now
remaining_min = int(remaining.total_seconds()/60)
return remaining_min
def arrival_time(planned_time, new_time):
if new_time.rstrip() != '':
return new_time.rstrip()
else:
return planned_time.rstrip()
def get_departues(rows):
from bs4 import BeautifulSoup as soup
if rows > 10:
raise ValueError('Can not handle more than 10 rows')
results = []
SUCCESSFUL_REQUEST_CODE = 200
payload ={
'dep1':'1',
'from':'16011351',
'to':'Sentrum (Trondheim)'
}
link = 'https://rp.atb.no/scripts/TravelMagic/TravelMagicWE.dll/svar'
r = requests.get(link, params=payload)
now = datetime.now()
if r.status_code is SUCCESSFUL_REQUEST_CODE:
soup = soup(r.content, 'html.parser')
arrival_times = soup.findAll('span', {'class':'tm-departurelist-time'})
busses = soup.findAll('strong', {'class':'tm-departurelist-linename'})
for bus, bustime in zip(busses[:rows], arrival_times[:rows]):
arrival = bustime.text.replace('\n','').replace(' ','')
remaining = remaining_minutes(arrival, now)
if remaining <= 15:
arrival = '{} min'.format(remaining)
results.append({'bus': bus.text.rstrip(),
'arrival': arrival.replace('\r','')})
else:
raise UserWarning('Error making request, status code: {}'
.format(r.status_code))
return results
if __name__ == "__main__":
results = get_departues(3)
for res in results:
print(res)