-
Notifications
You must be signed in to change notification settings - Fork 1
/
busscraper.py
62 lines (45 loc) · 1.64 KB
/
busscraper.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
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from functional import seq
import requests
from datetime import datetime
def parse_bus_soup(rows):
""" Parse bus rows to get the bus stop name and corresponding status
Args:
rows (list)
Returns
list
"""
return seq(rows)\
.map(lambda tr: seq(tr.select("td"))
.map(lambda td: td.text.strip()))
def get_update_time(soup, now):
"""Extract the update time from the soup and convert it into the datetime.datetime format
Args:
soup (bs4.BeautifulSoup)
now (datetime.datetime)
Returns:
datetime.datetime
"""
text = soup.find(attrs={"class": "updatetime"}).get_text(strip=True)
return datetime.strptime(text, '(更新時間:%H:%M:%S)').replace(now.year, now.month, now.day)
def get_bus_info(busnum):
"""Fetch and extract bus information given a bus number
Args:
busnum (str): The bus number in Taipei city
Returns:
dictionary: extracted information including
1. inbound/outbound status (str)
2. updatetime (datetime.datetime)
"""
now = datetime.now()
res = requests.get("http://pda.5284.com.tw/MQS/businfo2.jsp", params={"routename": busnum})
soup = BeautifulSoup(res.text, "html.parser")
info = {}
tbl = soup.select("table > tr > td > table > tr")[1].select("> td ")
inbound_tr = tbl[0].select("> table > tr")
outbound_tr = tbl[1].select("> table > tr")
info["inbound"] = parse_bus_soup(inbound_tr)
info["outbound"] = parse_bus_soup(outbound_tr)
info["updatetime"] = get_update_time(soup, now)
return info