-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
155 lines (131 loc) · 4.93 KB
/
t.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import time
import logging
import json
from bs4 import BeautifulSoup
from sendemail import sendEmail
# 配置log文件
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='lesson.log',
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
headers = {
"User-Agent": agent
}
# 学生信息
data = {
"zjh": "",
"mm": ""
}
param = {
"kcId": "306003010_01",
"preActionType": "5",
"actionType" : "9"
}
# 教务处ip
jwc_ip = "202.115.47.141"
time_delay = 1
sleeptime = 5
# 会话
s = requests.Session()
# login 负责更新Session的cookies和登录
# first_flag 用来标记是否是第一次登录
def login(first_flag = False):
global s
s.headers.update(headers)
while True:
try:
s.get("http://" + jwc_ip + "/loginAction.do", timeout = time_delay)
# 再访问一次加入cookies
r = s.get("http://" + jwc_ip + "/loginAction.do", timeout = time_delay)
# 登录
r = s.post("http://" + jwc_ip + "/loginAction.do", data = data, timeout = time_delay)
# 检查密码是否正确
r = s.get("http://" + jwc_ip + "/xkAction.do?actionType=6", timeout = time_delay)
if "错误信息" not in r.text:
break
else:
print("账号或密码不正确,请重新输入账号和密码...")
return 0 # 账号和密码不正确
except requests.exceptions.Timeout:
logging.info("网络有问题,正在重连...")
except requests.exceptions.ConnectionError as e:
logging.info(e)
logging.info("有可能是教务处宕机了,也有可能没联网...\n请检查网络后,重启程序")
time.sleep(1008611)
if first_flag:
logging.info("登陆成功,开始工作...")
return 1
def grade():
global s
global headers
try:
r = s.get("http://" + jwc_ip + "/gradeLnAllAction.do?type=ln&oper=fainfo&fajhh=2932", timeout = time_delay)
soup = BeautifulSoup(r.text)
table = soup.find(id='user')
body = table.tbody
courses = []
for tr in body.findAll('tr'):
one_course = tr.findAll('td')
course = {}
course['课程号'] = one_course[0].string.strip()
course['课序号'] = one_course[1].string.strip()
course['课程名'] = one_course[2].string.strip()
course['英文课程名'] = one_course[3].string.strip()
course['学分'] = one_course[4].string.strip()
course['课程属性'] = one_course[5].string.strip()
course['成绩'] = one_course[6].p.string.strip()
courses.append(course)
print("running")
print('-------------------')
info = ''
# 中途被顶掉登录了
if "请您登录后再使用" in r.text:
logging.info("重复登录,程序重新登陆...")
info = "重复登录,程序重新登陆..."
print(info)
login()
except requests.exceptions.Timeout:
logging.info("网络有问题,正在重连...")
login()
except requests.exceptions.ConnectionError as e:
logging.info(e)
logging.info("有可能是教务处宕机了,也有可能没联网...\n请检查网络后,重启程序")
time.sleep(1008611)
# 若不成功, 返回0
return courses
def normlize(courses):
all_course = ''
for course in courses:
s = course['课程名']+' '+course['学分']+' '+course['课程属性']+' '+course['成绩']+'\n'
all_course = all_course + s
return all_course
if __name__ == "__main__":
# 登录
while True:
f = open("student.json", encoding='utf-8')
global data
data = json.load(f)
if login(True) == 1:
break
firsttime = True # 第一次的话,就选快点,后面有个间隔就好
initial_courses = grade()
initial = len(initial_courses)
sendEmail('初始成绩', normlize(initial_courses))
while True:
courses = grade()
course_sum = len(courses)
if course_sum>initial:
# 发送短信
initial = course_sum
sendEmail('出成绩了', normlize(courses-initial_courses))
initial_courses = courses