This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleClass.py
107 lines (100 loc) · 3.91 KB
/
ScheduleClass.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
import sys
import requests
import re
import yaml
class ScheduleClass:
def __init__(self):
# 获取学号及密码
config_file = open(r'config/account.yaml', 'r', encoding='utf-8')
config_content = config_file.read()
self.config = yaml.load(config_content, Loader=yaml.FullLoader)
self.username = self.config['username']
self.password = self.config['password']
self.get_login_cookie()
self.cookie = ""
self.rank = []
self.schedule = []
self.success = 0
# 获取第一次cookie(匹配验证码提交cookie)
def get_login_cookie(self) -> dict:
"""
获取登录时所需要的cookie
:return: cookie
"""""
# 初次发送GET请求
first_request = requests.get("http://202.119.81.113:8080/")
# 获取访问的cookie
self.login_cookie = first_request.headers.get('Set-Cookie')[11:43]
self.login_cookie = {'JSESSIONID': self.login_cookie}
return self.login_cookie
# 获取新的验证码
def get_verify_code(self) -> requests.Response.content:
"""
通过登录时的cookie获取验证码图片
:return: 验证码图片
"""""
# 以获取的cookie获取新的验证码
image = requests.get("http://202.119.81.113:8080/verifycode.servlet", cookies=self.login_cookie)
return image.content
# 获取第二次cookie(教务系统页面cookie)
def login(self, verifycode):
"""
通过提交登录表单获取访问时所用的cookie
:param verifycode: 验证码的内容
:return: 无返回值
"""""
# 合成登录表单
useDogeCode = ""
login_form = {'USERNAME': self.username, 'PASSWORD': self.password, 'useDogeCode': useDogeCode,
'RANDOMCODE': verifycode}
# 发送POST请求
login = requests.post("http://202.119.81.113:8080/Logon.do?method=logon", data=login_form,
cookies=self.login_cookie)
# 获取新的cookie
if(len(login.history) <= 1):
self.success = 0
else:
self.success = 1
cookie_pair = login.history[1].headers.get('Set-Cookie')[11:43]
self.cookie = {'JSESSIONID': cookie_pair}
# 成绩查询
def get_rank(self) -> list:
"""
获取所有成绩列表
:return: 成绩列表
"""""
# 判断是否成功登录
if self.success == 1:
kksj = ''
kcxz = ''
kcmc = ''
xsfs = 'all'
# 根据页面的元素发送post请求
# kksj:开课时间,kcxz:课程性质,kcmc:课程名称,xsfs:显示方式
# 具体参数类型参照教务处html元素
rank_data = {'kksj': kksj, 'kcxz': kcxz, 'kcmc': kcmc, 'xsfs': xsfs}
# 发送post请求
get_rank = requests.get('http://202.119.81.112:9080/njlgdx/kscj/cjcx_list', data=rank_data, cookies=self.cookie)
get_rank.encoding = 'utf-8'
html = get_rank.text
# 获取表格内容
table = re.findall(r'<table(.*?)</table>', html, re.S)
rank_list = re.findall(r'<tr>(.*?)</tr>', table[1], re.S)
# 移除表头内容
rank_list.pop(0)
# 返回的数据集
data = []
# 截取每行内容
for i in range(len(rank_list)):
data.append(re.findall(r'<td(.*?)</td>', rank_list[i], re.S))
# 删除内容的css样式残余
for i in range(len(data)):
for j in range(len(data[i])):
str_list = data[i][j].split('>')
data[i][j] = str_list[1]
self.rank = data
return self.rank
else:
# 登录失败时的提示
result = ['请重试']
return result