-
Notifications
You must be signed in to change notification settings - Fork 0
/
TYUT.py
243 lines (223 loc) · 9.59 KB
/
TYUT.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
from urllib import request
import sys
import io
from selenium import webdriver
from urllib import parse
from bs4 import BeautifulSoup
import time
import json
import xlwt
import requests
import os
from getpass import getpass
headers = {} # 全局变量headers
student_name = "" # 全局变量,存储学生姓名
def get_index(lists,strs):
"""
该函数返回传经来的lists中的元素包含strs的元素索引
"""
index = []
for i in range(len(lists)):
if strs in lists[i]:
index.append(i)
return index
def get_html(url,data):
"""获取网页源代码"""
data = parse.urlencode(data).encode('gbk')
req = request.Request(url, data=data, headers=headers)
response = request.urlopen(req)
html = response.read().decode('utf-8')
return html
def login():
"""
实现用户的登陆
"""
print('*' * 21, '欢迎使用太理教务查询系统', '*' * 17)
print(' ' * 40, 'directed by Zhu Jiang')
print('*' * 64)
while True:
student_id = input('输入学号:') # 获取用户的学号
password = getpass("输入密码(身份证后6位):") # 该方法不会显示用户输入的密码,但是无法再IDE中的控制端运行,只能在cmd中实现
print("正在登录。。。") # 显示脚本状况
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') # 改变标准输出的默认编码
# 设置该options以使得selenium在运行是不会弹出浏览器框
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(chrome_options=chrome_options) # 初始化浏览器对象,options参数使得浏览器不会弹出
base_url = "http://jxgl.tyut.edu.cn:999/" # 登录页网站
browser.get(base_url) # 获取该网站信息
browser.find_element_by_css_selector(".form-username ").send_keys(student_id) # 帐号
browser.find_element_by_css_selector(".form-password ").send_keys(password) # 密码
browser.find_element_by_css_selector(".btn").click() # 登录
time.sleep(2) # 休眠2s来获取登陆后的页面,否则会返回登录页面的url
current_url = browser.current_url
if current_url != base_url: # 如果当前url和base_url不一样,说明登录成功
cookies = browser.get_cookies() # 获取登陆后用户的cookies
cookie_str = "" # 构造cookie并实现拼接
for item_cookie in cookies:
item_str = item_cookie["name"]+"="+item_cookie["value"]+"; "
cookie_str += item_str
global headers
# 将构造好的cookie放在headers并声明为全局变量以便后续的获取网页
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
"Cookie": cookie_str
}
# 获取网页信息并从中提取用户姓名
demo = requests.get(current_url,headers=headers).text
global student_name
soup = BeautifulSoup(demo,'lxml')
student_name = soup.select(".user-info")[0].get_text().split()[-1] # 根据网页源代码提取出登录用户的姓名
print()
print()
print("登录成功,", student_name + " 同学,请输入你想获取什么信息?")
browser.quit() # 登录成功,退出browser
return # 已经登录成功,跳出该函数
else:
print("用户名或密码错误,请重新输入!") # 输入错误后重新输入
def get_ranking():
"""
获取用户的排名情况
"""
try:
url = "http://jxgl.tyut.edu.cn:999/Tschedule/C6Cjgl/GetXskccjResult"
data = {'order': 'zxjxjhh desc,kch'}
html = get_html(url,data)
soup = BeautifulSoup(html,'lxml')
for i , j in zip(soup.select(".profile-info-name"),soup.select(".profile-info-value")):
print(i.get_text(),end=" "+j.get_text())
print()
except Exception:
print('获取成绩失败!')
print("不好意思同学,大概率是学校网站崩了,或者是学校网站改动了,我会尽快处理的。")
def get_grades():
"""获取用户的成绩信息"""
try:
url = 'http://jxgl.tyut.edu.cn:999/Tschedule/C6Cjgl/GetKccjResult'
data = {'order': 'zxjxjhh desc,kch'}
html = get_html(url,data)
soup = BeautifulSoup(html, 'lxml')
print("获取成绩成功!")
print()
courses = []
for tr in soup.select("tr"):
x = 0
course = []
for td in tr.select("td"):
if x == 2 or x == 6:
course.append(td.get_text())
elif '学年' in td.get_text():
course.append(td.get_text())
x += 1
courses.extend(course)
index = get_index(courses,'学年')
result_dict = {}
for i in range(len(index)):
if i+1!=len(index):
result_dict.setdefault(courses[index[i]],[]).extend(courses[index[i]+1:index[i+1]])
else:
result_dict.setdefault(courses[index[i]], []).extend(courses[index[i]+1:])
for i in result_dict:
print("*"*20)
print(i)
print()
turns = 0
for j in result_dict[i]:
if turns ==2:
print()
turns = 0
print(format(j,"^25"),end="")
turns += 1
print()
print()
except Exception:
print('获取成绩失败!')
print("不好意思同学,大概率是学校网站崩了,或者是学校网站改动了,我会尽快处理的。")
def get_course():
try:
filename = student_name + '同学的课程表.xlsx'
if os.path.exists(filename):
print()
print("你已经生成课表了,请到对应目录查看!")
return
print('正在获取课表信息,即将生成课表文件。。。')
url = 'http://jxgl.tyut.edu.cn:999/Tresources/A1Xskb/GetXsKb'
data = {'zxjxjhh': ''}
html = get_html(url,data)
# 清洗数据
soup = BeautifulSoup(html, 'lxml')
course_dict = soup.p.string
result = {}
for i in range(1, 6):
result[str(i)] = {}
x = json.loads(course_dict)
def course_detail(data):
result = []
result.append(data["Kcm"]) # 课程名
result.append(data["Zcsm"]) # 周长数名
result.append(data["Dd"]) # 地点
result.append(data['Jsm']) # 讲师名
result.append(data["Jc"]) # 上课时间
result.append(data['Skxq']) # 上课星期
return result
for i in x["rows"]:
res = course_detail(i) # 对每一个rows筛选出有用的数据
if res[-1] != None:
index = str(res[-1]) # index就是课程的星期
result[index].setdefault(res[-2], []).append(res[:-2]) # 以上课时间为键,其余信息为值创建字典
date = ['星期一', '星期二', '星期三', '星期四', '星期五']
course_time = ['1-2', '3-4', '5-6', '7-8']
f = open('course_dict.txt','w')
f.write(str(result))
f.close()
book = xlwt.Workbook(encoding='utf-8') # 创建excel表
sheet = book.add_sheet('sheet1') # excel表的sheet信息
style = xlwt.XFStyle() # 初始化样式
style.alignment.wrap = 1 # 自动换行
for i in range(8):
first_col = sheet.col(i) # xlwt中是行和列都是从0开始计算的
first_col.width = 256 * 20
tall_style = xlwt.easyxf('font:height 720;')
for i in range(12): # 36pt,类型小初的字号
first_row = sheet.row(i)
first_row.set_style(tall_style)
for i in range(len(date)): # 写入星期信息
sheet.write(0, i + 1, date[i], style)
for i in range(len(course_time)): # 写入上课时间信息
sheet.write(i + 1, 0, course_time[i], style)
for i in result:
for course_jie in result[i]:
strs = ""
for lengths in range(len(result[i][course_jie])):
strs += "\n".join(result[i][course_jie][lengths])
strs += "\n"
sheet.write(int(int(course_jie[-1]) / 2), int(i), strs, style)
book.save(filename)
print()
print("生成课表'"+filename+"'成功,请到对应目录查看!")
except Exception as err:
print('获取课表失败!')
print("不好意思同学,大概率是学校网站崩了,或者是学校网站改动了,我会尽快处理的。")
print(err)
def main():
login()
while True:
print()
get_input = input("[1]获取排名/[2]获取课程成绩/[3]获取课表[0]退出:")
if int(get_input) == 1:
get_ranking()
elif int(get_input) == 2:
get_grades()
elif int(get_input) == 3:
get_course()
elif int(get_input) == 0:
print()
print("退出成功,欢迎下次使用!")
break
else:
print()
print("同学,请输入1,2,3,0来选取对应内容!")
if __name__=="__main__":
main()