-
Notifications
You must be signed in to change notification settings - Fork 0
/
NKUST_Grab_A_Course.py
218 lines (218 loc) · 10.6 KB
/
NKUST_Grab_A_Course.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from bs4 import BeautifulSoup
import json
import requests
import random
import threading
import os
import time
class NKUST_Grab_A_Course:
def __init__(self,account,password):
self.UserAccount = account
self.UserPassword = password
self.r = requests.session()#建立一個session,讓每次request都算同一次,才能連續執行動作。
self.LoginPage_URL = "https://aais6.nkust.edu.tw/selcrs_std"#高科大學生選課系統登入首頁網址。
self.LoginPage_RVT = ""#LoginPage_RequestVerificationToken
self.LoginButtom_URL = "https://aais6.nkust.edu.tw/selcrs_std/Login"#按下登入時post的網址。
self.SelectPage_URL = "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/SelectPage"#高科大學生選課系統選課網址。
self.SelectPage_RVT = ""#SelectPage_RequestVerificationToken
self.CourseUnitCollection_URL = "https://aais6.nkust.edu.tw/selcrs_std/Unit/CourseUnitCollection"#請求下拉選單資訊網址
self.CourseUnitCollection_Data = ""#儲存選單資訊
self.CourseSearch_URL = "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/CourseSearch"#請求查詢課程資訊網址
self.CourseSearch_Data = ""#儲存課程資訊
self.AddPreSelCrs_URL = "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/AddPreSelCrs"#請求新增課程網址
self.AddPreSelCrs_Data = ""
def Get_LoginPage_RVT(self):
request_out = 5
head = {
"referer" : "https://aais5.nkust.edu.tw/selcrs_std/PreselecrsList",
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
}
while(request_out != 0):
try:
LoginPage_Html = self.r.get(self.LoginPage_URL, headers = head, timeout = 1)
#對伺服器發出get,取得登入頁面。
#print(LoginPage_Html)
#確認是否請求成功 200
LoginPage_Html.encoding = "utf-8"#編碼成utf-8
#print(LoginPage_Html.text)
#未解析的資料是一大串字串,難以抓取資料,因此需要解析。
LoginPage_Soup = BeautifulSoup(LoginPage_Html.text, 'html.parser')
#解析LoginPage_Html。
#print(LoginPage_Soup)
#查看解析後內容
LoginPage_Input = LoginPage_Soup.select("input")
#抓取頁面中為input的標籤。
#print("RVT :",LoginPage_Input[1]["value"])
#該頁面第二個input有我們需要的RequestVerificationToken值。
self.LoginPage_RVT = LoginPage_Input[1]["value"]
#儲存到LoginPage_RVT。
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
def Login(self):
request_out = 5
head = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
"content-type" : "application/x-www-form-urlencoded",
"origin" : "https://aais5.nkust.edu.tw",
"referer" : "https://aais5.nkust.edu.tw/selcrs_std"
}
data = {
"__RequestVerificationToken": self.LoginPage_RVT,
"Url": "/selcrs_std/PreselecrsList",
"UserAccount": self.UserAccount,
"Password": self.UserPassword
}
while(request_out != 0):
try:
LoginButtom = self.r.post(self.LoginButtom_URL,data=data, headers = head, timeout = 1)
#對伺服器發出post,達成登入動作。
#print(LoginButtom)
#確認是否請求成功 200
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
def Get_SelectPage_RVT(self):
request_out = 5
head = {
"referer" : "https://aais5.nkust.edu.tw/selcrs_std/Home/About",
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
}
while(request_out != 0):
try:
SelectPage_Html = self.r.get(self.SelectPage_URL, headers = head, timeout = 1)
#對伺服器發出get,取得選課頁面。
#print(SelectPage_Html)
#確認是否請求成功 200
SelectPage_Html.encoding = "utf-8"#編碼成utf-8
#print(SelectPage_Html.text)
#未解析的資料是一大串字串,難以抓取資料,因此需要解析。
SelectPage_Soup = BeautifulSoup(SelectPage_Html.text, 'html.parser')
#解析SelectPage_Html。
#print(SelectPage_Soup)
#查看解析後內容
SelectPage_Input = SelectPage_Soup.select("input")
#抓取頁面中為input的標籤。
#print("RVT :",SelectPage_Input[len(SelectPage_Input)-1]["value"])
#該頁面最後一個input有我們需要的RequestVerificationToken值。
self.SelectPage_RVT = SelectPage_Input[len(SelectPage_Input)-1]["value"]
#儲存到SelectPage_RVT。
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
def CoursUnitCollection(self):
request_out = 5
head={
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"origin": "https://aais6.nkust.edu.tw",
"referer": "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/SelectPage",
"requestverificationtoken": self.SelectPage_RVT,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"x-requested-with": "XMLHttpRequest"
}
data = {
"CrsType": "A",
"ShortTerm": "N"
}
while(request_out != 0):
try:
CourseUnitCollection = self.r.post(self.CourseUnitCollection_URL ,data=data, headers = head, timeout = 0.5)
#對伺服器發出post,取得下拉選單資訊。
self.CourseUnitCollection_Data = json.loads(CourseUnitCollection.text)
#將資料存成json型態
#print(self.CourseUnitCollection_Data[0])
#印出第一筆資料來查看是否有成功
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
def CourseSearch(self,number = ""):
request_out = 5
head={
"Content-Type": "application/json; charset=UTF-8",
"origin": "https://aais6.nkust.edu.tw",
"referer": "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/SelectPage",
"requestverificationtoken": self.SelectPage_RVT,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"x-requested-with": "XMLHttpRequest"
}
data = {
"SearchViewModel":{#在此你可以依據取得的下拉表單資訊帶入查詢,我僅用課程代碼示範。
"cmp_area":"1",
"dgr_id":"14",
"unt_id":"UE29",
"cls_year":"3",
"cls_seq":"ALL",
"scr_selcode": number,#課程代碼,當課程代碼不為""時,僅查詢課程代碼。
"scr_language":"",
"scr_time":"",
"CrsType":"N"
}
}
while(request_out != 0):
try:
CourseSearch = self.r.post(self.CourseSearch_URL ,data=json.dumps(data), headers = head, timeout = 0.5)
#對伺服器發出post,取得課程資訊。
self.CourseSearch_Data = json.loads(CourseSearch.text)
#將資料存成json型態
#print(self.CourseSearch_Data["recordsTotal"])
#總共查詢到幾個課程
#print(self.CourseSearch_Data["data"][0])
#第一筆課程資訊,以課程代碼查詢僅會查詢到一筆。
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
def AddPreSelCrs(self,number = ""):
request_out = 5
head={
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"origin": "https://aais6.nkust.edu.tw",
"referer": "https://aais6.nkust.edu.tw/selcrs_std/FirstSelect/SelectPage",
"requestverificationtoken": self.SelectPage_RVT,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"x-requested-with": "XMLHttpRequest"
}
data = {
"CrsNo": number,#選課代號
"PCrsNo": self.CourseSearch_Data["data"][0]['scj_sub_percode'],#可用剛剛查詢課程資訊的函示查詢該筆資料
"SelType": "O",
"CrsType": self.CourseSearch_Data["data"][0]['CrsType'],
"NewClass": "N"
}
while(request_out != 0):
try:
AddPreSelCrs = self.r.post(self.AddPreSelCrs_URL ,data=data, headers = head, timeout = 0.5)
#對伺服器發出post,新增課程。
self.AddPreSelCrs_Data = json.loads(AddPreSelCrs.text)
print(self.AddPreSelCrs_Data["result"])
print(self.AddPreSelCrs_Data["MessageBotton"])
print(self.AddPreSelCrs_Data["MessageShow"])
#印出資訊查看是否有新增成功
return True
except:
time.sleep(random.randint(1,3))
request_out = request_out - 1
continue
return False
if __name__ == '__main__':
Start = NKUST_Grab_A_Course("C108152305","abc900403")
Start.Get_LoginPage_RVT()
Start.Login()
Start.Get_SelectPage_RVT()
Start.CoursUnitCollection()
Start.CourseSearch("0988")
Start.AddPreSelCrs("0988")