forked from andyjjrt/NCCUCourse
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchRemain.py
More file actions
67 lines (55 loc) · 2.72 KB
/
fetchRemain.py
File metadata and controls
67 lines (55 loc) · 2.72 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
62
63
64
65
66
67
import logging
import requests
from bs4 import BeautifulSoup
PROPERTY_NAME = {
"專業基礎(開放系所)人數": "origin",
"其他系所": "otherDept",
"總人數": "all",
"本系本班Dept./Class": "origin",
"本系非本班同年級Other Classes in Dept., Same Year": "sameGrade",
"本系非本班不同年級Other Classes in Dept., Dif. Year": "diffGrade",
"輔系Minor": "minor",
"雙主修Double-Major": "doubleMajor",
"全系Dept.": "origin",
"本院非本系Other Depts. in the College": "otherDeptInCollege",
"非本院Other Colleges": "otherCollege",
"學分學程": "program",
"全校All Colleges": "all",
"本學程開課年級(含)以上Same Year (and above) in the Program": "sameGradeAndAbove",
"本學程其他低年級Year Below you in the Program": "lowerGrade",
"本院非學程限制人數Maximum Limits for Other Programs in the College": "otherProgram",
"外院限制人數Maximum Limits for Other Colleges": "otherCollege",
"總限制人數Overall Maximum Limits": "all",
}
ROW_NAME = {
"限制人數 / Maximum limit": "Limit",
"選課人數 / Number Registered": "Registered",
"餘額 / Number of Available Spaces": "Available"
}
def fetchRemain(fetch_url: str):
result = {prop+row: None for prop in PROPERTY_NAME.values() for row in ROW_NAME.values()}
try:
response = requests.get(fetch_url.replace("https://", "http://"))
response.raise_for_status()
soap = BeautifulSoup(response.content, "html.parser")
signable_element = soap.select_one("#Open_to_signable_addingL")
if signable_element:
result["signableAdding"] = signable_element.get_text(strip=True) == "是"
else:
result["signableAdding"] = None
table = soap.find("div", {"class": "maintain_profile_content_table"}).find_all("tr")
number_on_waiting_list = table[6].find_all("td")[1].find("a").text
result["waitingList"] = int(number_on_waiting_list) if number_on_waiting_list.isdigit() else number_on_waiting_list
table = soap.find("table", {"id": "tclmtcntGV"}).find_all("tr")
for _, prop in enumerate(table[0]):
if prop.get_text(strip=True) != "":
for row in table[1:]:
cells = row.find_all("td")
for td in cells:
result[PROPERTY_NAME[prop.text] + ROW_NAME[cells[0].text]] = int(td.text) if td.text.isdigit() else td.text
except Exception as e:
logging.error(e)
return result
if __name__ == "__main__":
print(fetchRemain(
"https://selectcourse.nccu.edu.tw/remain/goGenDetail.aspx?view=7735414C415774495851503054646C41713573494F513D3D"))