This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy path东南大学选课助手_offline.py
401 lines (358 loc) · 16.4 KB
/
东南大学选课助手_offline.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# coding: utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import random
import time
import datetime
import os
import sys
import msvcrt
from PIL import Image
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
def pwd_input(): # 密码输入
chars = []
while True:
newChar = msvcrt.getch().decode(encoding="utf-8")
if newChar in '\r\n':
break
elif newChar == '\b':
if chars:
del chars[-1]
msvcrt.putch('\b'.encode(encoding='utf-8'))
msvcrt.putch( ' '.encode(encoding='utf-8'))
msvcrt.putch('\b'.encode(encoding='utf-8'))
else:
chars.append(newChar)
msvcrt.putch('*'.encode(encoding='utf-8'))
return (''.join(chars) )
def Login(): # 登录
global t
global msg
global error
global username
global password
global teacher
global driver
try:
# 启动浏览器
driver = webdriver.Chrome(executable_path='chromedriver', chrome_options=chrome_options)
driver.maximize_window()
driver.set_window_size(500, 10000)
url = "http://newxk.urp.seu.edu.cn/xsxk/profile/index.html"
driver.get(url)
os.system('cls')
print("开始登录\n")
username = input("用户名:")
print("密 码:",end="")
password = pwd_input()
print()
# 截取验证码
# verifycodepic = driver.find_element_by_xpath('//*[@id="vcodeImg"]')
# location = verifycodepic.location
# size = verifycodepic.size
# print(location, size)
screenshot = driver.get_screenshot_as_file('verficode.png')
imgOrigin = Image.open('verficode.png')
img=imgOrigin.crop((315,332,315+129,332+42))
img.show()
verifycode = input("验证码:")
os.remove('verficode.png')
# 模拟输入与点击
driver.find_element_by_xpath(
'//*[@id="loginNameDiv"]/div/input').click()
driver.find_element_by_xpath(
'//*[@id="loginNameDiv"]/div/input').send_keys(username)
driver.find_element_by_xpath(
'//*[@id="loginPwdDiv"]/div/input').click()
driver.find_element_by_xpath(
'//*[@id="loginPwdDiv"]/div/input').send_keys(password)
driver.find_element_by_xpath(
'//*[@id="verifyCode"]').click()
driver.find_element_by_xpath(
'//*[@id="verifyCode"]').send_keys(verifycode)
print("\n登陆中,请稍后.", end="")
time.sleep(1)
driver.find_element_by_xpath('//*[@id="loginDiv"]/button').click()
print(".", end="")
time.sleep(2)
elecTurn=input("\n\n选课轮次:")
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div[4]/div/div[2]/div/table/tbody/tr['+str(elecTurn)+']/td/div/div/div[6]/div[2]/label/span[1]/span').click()
print(".", end="")
time.sleep(1)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div[4]/div/div[3]/span/button[1]').click()
print(".", end="")
time.sleep(1)
driver.find_element_by_xpath(
'//*[@id="stundentinfoDiv"]/button').click()
print(".", end="")
time.sleep(2)
print(".\n\n")
checkUrl = driver.current_url
print("\n"+checkUrl+"\n")
if not checkUrl.startswith("http://newxk.urp.seu.edu.cn/xsxk/elective/"):
print(username + '\t' + password + '\t登录失败')
msg += username + '\t' + password + '\t登录失败\n\n'
error = True
# driver.quit()
return
os.system('cls')
print("成功登录!\n")
return
except Exception as e:
msg += '登录失败' + '\n\n' + str(e) + '\n\n'
print('\t登录失败' + '\n' + str(e))
error = True
# driver.quit()
return
def main():
try:
class_wanted = input("输入课程号:")
finded = False
# screenshot = driver.get_screenshot_as_file('a.png')
# img = Image.open('a.png')
# img.show()
print("查找中.", end="")
# 系统推荐课程
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[1]/i').click()
time.sleep(0.5)
print(".", end="")
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[2]').click()
time.sleep(1)
print(".", end="")
curpages = 1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(str(curpages))
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
while str(pages) == str(curpages) and not finded:
class_list = driver.find_elements_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[1]/div')
# print("classlist:")
# print(class_list)
# print("\n")
for cl in class_list:
class_num = cl.find_element_by_xpath(
'.//*[@class="el-card__body"]/div[2]/div/div[2]/span').text
# print("即将查找"+class_wanted[0:-6]+"课程\n")
print(".", end="")
if class_wanted[0:-5] == class_num:
# print("查找到"+class_wanted[0:-6]+"课程\n")
cl.click()
time.sleep(0.2)
print(".", end="")
teacher_list=cl.find_elements_by_xpath(
'.//*[@class="card-list course-jxb el-row"]/div')
# print("即将查找课程号"+class_wanted[-3:-1])
for tl in teacher_list:
print(".", end="")
teacher_num=tl.find_element_by_xpath(
'.//*[@class="card-item head"]/div[1]/span[1]').text
# print("正在查找课程号"+teacher_num[1:3])
if class_wanted[-3:-1]==teacher_num[1:3]:
print("\n\n已找到\n")
finded = True
Turn=1
elected=False
while not elected:
print("第"+str(Turn)+"次尝试")
Turn=Turn+1
tl.find_element_by_xpath(
'.//*[@class="el-row"]/button[2]').click()
try:
driver.find_element_by_xpath(
'/html/body/div[3]/div/div[3]/button[2]').click()
except Exception as eEle:
elected=True
time.sleep(2)
os.system('cls')
os.system('cls')
print("\n已成功选上!\n")
break
break
if not finded:
curpages = curpages+1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys('%d' %curpages)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
# 体育项目
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[1]/i').click()
time.sleep(0.5)
print(".", end="")
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[5]').click()
time.sleep(1)
print(".", end="")
curpages = 1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(str(curpages))
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
while str(pages) == str(curpages) and not finded:
class_list = driver.find_elements_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[1]/div')
# print("classlist:")
# print(class_list)
# print("\n")
for cl in class_list:
class_num = cl.find_element_by_xpath(
'.//*[@class="el-card__body"]/div[2]/div/div[2]/span').text
# print("即将查找"+class_wanted[0:-6]+"课程\n")
print(".", end="")
if class_wanted[0:-5] == class_num:
# print("查找到"+class_wanted[0:-6]+"课程\n")
cl.click()
time.sleep(0.2)
print(".", end="")
teacher_list=cl.find_elements_by_xpath(
'.//*[@class="card-list course-jxb el-row"]/div')
# print("即将查找课程号"+class_wanted[-3:-1])
for tl in teacher_list:
print(".", end="")
teacher_num=tl.find_element_by_xpath(
'.//*[@class="card-item head"]/div[1]/span[1]').text
# print("正在查找课程号"+teacher_num[1:3])
if class_wanted[-3:-1]==teacher_num[1:3]:
print("\n\n已找到\n")
finded = True
Turn=1
elected=False
while not elected:
print("第"+str(Turn)+"次尝试")
Turn=Turn+1
tl.find_element_by_xpath(
'.//*[@class="el-row"]/button[2]').click()
try:
driver.find_element_by_xpath(
'/html/body/div[3]/div/div[3]/button[2]').click()
except Exception as eEle:
elected=True
time.sleep(2)
os.system('cls')
os.system('cls')
print("\n已成功选上!\n")
break
break
if not finded:
curpages = curpages+1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys('%d' %curpages)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
# 通选课
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[1]/i').click()
time.sleep(0.5)
print(".", end="")
driver.find_element_by_xpath('//*[@id="xsxkapp"]/div/div[1]/ul/li[6]').click()
time.sleep(1)
print(".", end="")
curpages = 1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(str(curpages))
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
while str(pages) == str(curpages) and not finded:
class_list = driver.find_elements_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[1]/div')
# print("classlist:")
# print(class_list)
# print("\n")
for cl in class_list:
class_num = cl.find_element_by_xpath(
'.//*[@class="el-card__body"]/div[2]/div/div[2]/span[1]').text\
+ " "\
+ cl.find_element_by_xpath(
'.//*[@class="el-card__body"]/div[2]/div/div[2]/span[2]').text
# print("查找到"+class_num+"课程\n")
print(".", end="")
if class_wanted == class_num:
print("\n\n已找到\n")
finded = True
Turn=1
elected=False
while not elected:
print("第"+str(Turn)+"次尝试")
Turn=Turn+1
tl.find_element_by_xpath(
'.//*[@class="el-row"]/button[3]').click()
try:
driver.find_element_by_xpath(
'/html/body/div[3]/div/div[3]/button[2]').click()
except Exception as eEle:
elected=True
time.sleep(2)
os.system('cls')
os.system('cls')
print("\n已成功选上!\n")
break
if not finded:
curpages = curpages+1
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[2]/div/input').send_keys('%d' %curpages)
driver.find_element_by_xpath(
'//*[@id="xsxkapp"]/div/div[3]/div[3]/div/div[2]/span[1]').click()
time.sleep(1)
print(".", end="")
pages = driver.find_element_by_class_name('number.active').text
# print("查找第"+pages+"页\n")
if not finded:
print("\n\n没找到!\n")
return
except Exception as e:
print('\t选课中止' + '\n' + str(e))
error = True
# driver.quit()
return
if __name__ == '__main__':
error = False
Login()
if not error:
main()