Huge Lemon的博客

Python 3.6 监控爬取成绩并推送到邮箱

2018-07-25

简介

本程序模拟登录正方教务系统获取本人当前学期成绩(也可以手动改学期学年),20分钟检测一次,当有新成绩公布时就发送到邮箱,本程序用到的是我学校的教务网网址。

  • 由于正方网页的每一项都需要cookie来进行访问,在用Chrome单独打开子页面时,网页会直接跳到登录前界面。在用调试查看了post后,发现cookie消失了;但是直接从网页上打开子网页可以访问。所以在登录时就要保存登录信息cookie。
  • 爬取部分是根据网上代码内容修改的,后面的监控和发送是自己完成的。
  • 本程序测试了多次修改了许许多多的bug,终于得以发布。
  • 该程序的发送邮箱部分需要用到Python SMTP功能,具体可参照xsanpython3通过qq邮箱发送邮件
  • 教务系统网址防止访问重复冲突,在后面加了一串hash码,所有的网址一定要带上它,这样才能访问到登录后的界面,否则没有cookie。
  • 开头要求用户输入本学期所有考试数目,包括重修、公选、跨选等课程,目的是在最后一次推送前将平均绩点算出并发送到邮箱。

源代码

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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
'''
大连大学成绩查询助手V3.5
Coded By Martin Huang
Code Changed By ZC Liang
2018.6.6
'''
import re
import urllib.request
import urllib.parse
import http.cookiejar
import bs4
import getpass
import pickle
import os
import platform
import subprocess
from bs4 import BeautifulSoup
from prettytable import PrettyTable
from PIL import Image
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import time
import prettytable as pt
import pandas as pd
import numpy as np

my_sender = '发件人邮箱账号' # 发件人邮箱账号
my_pass = '发件人邮箱密码' # 发件人邮箱密码(当时申请smtp给的口令)
# my_user = '收件人邮箱账号' # 收件人邮箱账号,我这边发送给自己
email_send_to = '' # 收件人邮箱账号

DstDir = os.getcwd()
searchCount = 0 # 查询次数
count = 0 # 循环计数
scorenum = 0 # 成绩条数
score = []
scorenp = np.array(score)
makeup_course_num = 0 # 重修课程数目
makeup_course_flag = -1 # 重修课程数目下标
courseList = []
required_course_num = 0

# 准备Cookie和opener,因为cookie存于opener中,所以以下所有网页操作全部要基于同一个opener
cookie = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(
urllib.request.HTTPCookieProcessor(cookie))

final_url = "" # 头 + 随机编码 + default2.aspx
final_url_head = ""
url_head = "202.199.155." + str(random.randint(33, 37)) # 随机产生网址

ddlxn = ""
ddlxq = ""

# 判断操作系统类型


def getOpeningSystem():
return platform.system()

# 判断是否联网


def isConnected():
userOs = getOpeningSystem()
if userOs == "Windows":
subprocess.check_call(
["ping", "-n", "2", url_head], stdout=subprocess.PIPE)
else:
subprocess.check_call(
["ping", "-c", "2", url_head], stdout=subprocess.PIPE)

# 获取重定向编码


def check_for_redirects(url):
r = requests.head(url)
if r.ok:
return r.headers['location']
else:
return '[no redirect]'

# 登陆


def login():
# 构造表单
params = {
'txtUserName': sid,
'Textbox1': '',
'Textbox2': spwd,
'RadioButtonList1': '学生',
'Button1': '',
'lbLanguage': '',
'hidPdrs': '',
'hidsc': '',
}
# 获取验证码
res = opener.open(final_url_head + '/checkcode.aspx').read()
with open('' + DstDir + '\\ScoreHelper\\CheckCode.jpg', 'wb') as file:
file.write(res)
img = Image.open('' + DstDir + '\\ScoreHelper\\CheckCode.jpg')
img.show()
vcode = input('请输入验证码:')
img.close()
params['txtSecretCode'] = vcode
# 获取ViewState
response = urllib.request.urlopen('http://' + url_head + '/')
html = response.read().decode('gb2312')
viewstate = re.search(
'<input type="hidden" name="__VIEWSTATE" value="(.+?)"', html)
params['__VIEWSTATE'] = viewstate.group(1)
# 尝试登陆
loginurl = final_url
print("\n本次登录所用网址为:" + loginurl + "\n")
data = urllib.parse.urlencode(params).encode('gb2312')
response = opener.open(loginurl, data)
if response.geturl() == final_url:
print('登陆失败,可能是姓名、学号、密码、验证码填写错误!')
return False
else:
return True


# 获取本学期必修课数目


def get_RequiredCourse_num():
global required_course_num

print("正在查询本学期必修课数目...")
# 构造url
url = ''.join([
final_url_head + '/xsxkqk.aspx',
'?xh=',
sid,
'&xm=',
urllib.parse.quote(sname),
'&gnmkdm=N121615',
])
# 构建查询学生选课情况表单
params = {
'ddlxn': ddlxn,
'ddlxq': ddlxq,
}
# 构造Request对象,填入Header,防止302跳转,获取新的View_State
req = urllib.request.Request(url)
req.add_header('Referer', final_url)
req.add_header('Origin', 'http://' + url_head + '/')
req.add_header(
'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
response = opener.open(req)
html = response.read().decode('gb2312')
viewstate = re.search(
'<input type="hidden" name="__VIEWSTATE" value="(.+?)"', html)
params['__VIEWSTATE'] = viewstate.group(1)
# 查询所有成绩
req = urllib.request.Request(
url, urllib.parse.urlencode(params).encode('gb2312'))
req.add_header('Referer', final_url)
req.add_header('Origin', 'http://' + url_head + '/')
response = opener.open(req)
soup = BeautifulSoup(response.read().decode('gb2312'), 'html.parser')
html = soup.find('table', class_='datelist')
# 指定要输出的列,原网页的表格列下标从0开始
# 用于标记是否是遍历第一行
flag = True
# 根据DOM解析所要数据,首位的each是NavigatableString对象,其余为Tag对象
# 遍历行
counter = 0
for each in html:
columnCounter = 0
column = []

if(type(each) == bs4.element.NavigableString):
pass
else:
# 遍历列
for item in each.contents:
if(item != '\n'):
if(counter>0 and columnCounter == 3):
courseList.append(str(item.contents[0]).strip())
columnCounter += 1
if flag:
flag = False
counter += 1

for each in courseList:
if(each == "必修课程"):
required_course_num += 1




# 获取成绩


def getScore():
global searchCount
global scorenum
global scorenp
global ddlxn
global ddlxq
score = []
# 构造url
url = ''.join([
final_url_head + '/xscjcx_dq.aspx',
'?xh=',
sid,
'&xm=',
urllib.parse.quote(sname),
'&gnmkdm=N121605',
])
# 构建查询全部成绩表单
params = {
'ddlxn': ddlxn, # 全部为 %C8%AB%B2%BF
'ddlxq': ddlxq,
'btnCx': '+%B2%E9++%D1%AF+',
}
# 构造Request对象,填入Header,防止302跳转,获取新的View_State
req = urllib.request.Request(url)
req.add_header('Referer', final_url)
req.add_header('Origin', 'http://' + url_head + '/')
req.add_header(
'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
response = opener.open(req)
html = response.read().decode('gb2312')
viewstate = re.search(
'<input type="hidden" name="__VIEWSTATE" value="(.+?)"', html)
params['__VIEWSTATE'] = viewstate.group(1)
# 查询所有成绩
req = urllib.request.Request(
url, urllib.parse.urlencode(params).encode('gb2312'))
req.add_header('Referer', final_url)
req.add_header('Origin', 'http://' + url_head + '/')
response = opener.open(req)
soup = BeautifulSoup(response.read().decode('gb2312'), 'html.parser')
html = soup.find('table', class_='datelist')
print("执行第" + str(searchCount) + "次查询:")
print('你的所有成绩如下:')
# 指定要输出的列,原网页的表格列下标从0开始
outColumn = [3, 4, 6, 7, 9, 11, 13]
# 用于标记是否是遍历第一行
flag = True
# 根据DOM解析所要数据,首位的each是NavigatableString对象,其余为Tag对象
# 遍历行
for each in html:
columnCounter = 0
column = []

if(type(each) == bs4.element.NavigableString):
pass
else:
# 遍历列
for item in each.contents:
if(item != '\n'):
if columnCounter in outColumn:
# 要使用str转换,不然陷入copy与deepcopy的无限递归
column.append(str(item.contents[0]).strip())
columnCounter += 1
if flag:
table = PrettyTable(column)
flag = False
else:
table.add_row(column)
score.extend([column])
searchCount += 1
scorenp = np.array(score)
# table.set_style(pt.PLAIN_COLUMNS)
print(table)
print("分条统计:")
scorenum = sendScore(table)
print("成绩数目: " + str(scorenum) + "条")


def sendScore(table):
global scorenum
global count
global email_send_to
global scorenp
for i in table:
print(i.get_string())
count += 1

if(count > scorenum):
try:
scorenum = count

# 文本模式
# context = i.get_string().replace("+"," ")
# context = context.replace("-"," ")
# context = context.replace("2017 2018","2017-2018")
# if(scorenum == 1):
# msg=MIMEText("有成绩下来了:" + context,'plain','utf-8')
# else:
# msg=MIMEText("又有成绩下来了:" + context,'plain','utf-8')
# msg = prettyScore()

# html格式
msg = prettyScore()

# 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['From'] = formataddr(["1115810371@qq.com", my_sender])
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['To'] = formataddr([email_send_to, email_send_to])
if(count == required_course_num):
msg['Subject'] = "第" + \
str(count) + "次成绩推送加平均绩点" # 邮件的主题,也可以说是标题
else:
msg['Subject'] = "第" + str(count) + "次成绩推送" # 邮件的主题,也可以说是标题
# 发件人邮箱中的SMTP服务器,端口是465
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
# 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.sendmail(my_sender, [email_send_to, ], msg.as_string())
server.quit() # 关闭连接
print("发送成功,请注意在此邮箱查收:" + email_send_to)
except Exception as e:
print(e)
print("发送失败")
count = 0
if(scorenum != required_course_num):
print("程序休息中...(按'Ctrl C'结束)")
time.sleep(1200) # 二十分钟查一次
return scorenum


def prettyScore():
global scorenp
try:
# context = MIMEText(html,_subtype='html',_charset='utf-8') #解决乱码
msg = MIMEText(str(htmlText(scorenum)), "html", "gb2312")
except Exception as e:
print(e)
return msg


def htmlText(scorenum):
global required_course_num

if(scorenum == required_course_num):
html = """

<table color="CCCC33" width="800" border="1" cellspacing="0" cellpadding="5" text-align="center">

<tr>

<td text-align="center">课程名称</td>

<td text-align="center">课程性质</td>

<td text-align="center">学分</td>

<td text-align="center">平时成绩</td>

<td text-align="center">期末成绩</td>

<td text-align="center">成绩</td>

</tr>


""" + addtrs(scorenum) + """
</table>
<div><h2>-->平均绩点:%s --<</h2></div>
""" % (getGPA())
else:
html = """

<table color="CCCC33" width="800" border="1" cellspacing="0" cellpadding="5" text-align="center">

<tr>

<td text-align="center">课程名称</td>

<td text-align="center">课程性质</td>

<td text-align="center">学分</td>

<td text-align="center">平时成绩</td>

<td text-align="center">期末成绩</td>

<td text-align="center">成绩</td>

</tr>


""" + addtrs(scorenum)
return html


def addtrs(scorenum):
global scorenp
i = 1
array = []
while(i <= scorenum):
trs = '''
<tr>

<td text-align="center">%s </td>

<td>%s </td>

<td>%s </td>

<td>%s </td>

<td>%s </td>

<td>%s </td>

</tr>
''' % (scorenp[i][0], scorenp[i][1], scorenp[i][2], scorenp[i][3], scorenp[i][4], scorenp[i][5])
array.append(trs)
i += 1
s = ""
for x in array:
s += str(x)
return s


def getGPA():
global scorenp
global scorenum
global makeup_course_num
global makeup_course_flag

sc = []
GPAlist = []
i = 1
j = 0
coursenum = 0

while(i <= scorenum):
if(scorenp[i][1] != "必修课程" or scorenp[i][6] == "是"):
makeup_course_num += 1
i += 1
continue
else:
if(scorenp[i][5] == "F"):
sc.append(0)
elif(scorenp[i][5] == "A"):
sc.append(95)
elif(scorenp[i][5] == "B"):
sc.append(85)
elif(scorenp[i][5] == "C"):
sc.append(75)
elif(scorenp[i][5] == "D"):
sc.append(65)
else:
sc.append(int(scorenp[i][5]))

if(int(sc[j]) < 60):
GPAlist.append(0)
else:
GPAlist.append((int(sc[j]) - 50)/10*float(scorenp[i][2]))
i += 1
j += 1
coursenum += 1

i = 1
j = 0
sum = 0
scoresum = 0

while(i <= scorenum):
if(scorenp[i][1] != "必修课程" or scorenp[i][6] == "是"):
i += 1
continue
sum += GPAlist[j]
scoresum += float(scorenp[i][2])
j += 1
i += 1
GPA = sum/scoresum
print("平均绩点:" + str(GPA))
return GPA


if __name__ == '__main__':
try:
localtime = time.localtime(time.time()) # 获取当前日期
if(int((localtime.tm_mon) >= 9 and int(localtime.tm_mon) <= 12) or (int(localtime.tm_mon) >= 1 and int(localtime.tm_mon) <= 2)):
if(str(localtime.tm_year) == "2020"):
print("您已毕业,无须监控成绩!")
sys.exit(0)
ddlxn = str(localtime.tm_year) + '-' + str(int(localtime.tm_year) + 1)
ddlxq = '1'
else:
ddlxn = str(int(localtime.tm_year) - 1) + '-' + str(localtime.tm_year)
ddlxq = '2'

searchCount = 1
print('欢迎使用大连大学成绩查询助手!')
print('正在检查网络...')
isConnected()
with open(r'' + DstDir + '\\ScoreHelper\\uinfo.bin', 'rb') as file:
udick = pickle.load(file)
sname = udick['sname']
sid = udick['sid']
spwd = udick['spwd']
email_send_to = udick['email_send_to']
final_url = 'http://' + url_head + \
check_for_redirects('http://' + url_head + '/default2.aspx')
final_url_head = final_url[0:48]
while(not login()):
continue
get_RequiredCourse_num()
getScore()
counter = 0
while(scorenum <= required_course_num):
counter += 1
if(scorenum == required_course_num):
print("本学期成绩查询完成!")
break
if(counter > 0):
getScore()
except FileNotFoundError:
# if os.path.exists(r'' + DstDir + '\\ScoreHelper'):
# os.remove(r'' + DstDir + '\\ScoreHelper')
os.mkdir(r'' + DstDir + '\\ScoreHelper') # 注:针对Windows目录结构
print('这是你第一次使用,请按提示输入信息,以后可不必再次输入~')
sid = input('请输入学号:')
sname = input('请输入姓名:')
# 隐藏密码
# spwd = getpass.getpass('请输入密码:')
spwd = input('请输入密码:')
email_send_to = input('请输入要将成绩发送到的邮箱地址:')
udick = {'sname': sname, 'sid': sid,
'spwd': spwd, 'email_send_to': email_send_to}
file = open(r'' + DstDir + '\\ScoreHelper\\uinfo.bin', 'wb')
pickle.dump(udick, file)
file.close()
final_url = 'http://' + url_head + \
check_for_redirects('http://' + url_head + '/default2.aspx')
final_url_head = final_url[0:48]
while(not login()):
sname = input('请输入姓名:')
sid = input('请输入学号:')
# spwd = getpass.getpass('请输入密码:')
spwd = input('请输入密码:')
email_send_to = input('请输入要将成绩发送到的邮箱地址:')
udick = {'sname': sname, 'sid': sid,
'spwd': spwd, 'email_send_to': email_send_to}
file = open(r'' + DstDir + '\\ScoreHelper\\uinfo.bin', 'wb')
pickle.dump(udick, file)
file.close()
final_url = 'http://' + url_head + \
check_for_redirects('http://' + url_head + '/default2.aspx')
final_url_head = final_url[0:48]
get_RequiredCourse_num()
getScore()
counter = 0
while (scorenum <= required_course_num):
counter += 1
if (scorenum == required_course_num):
print("本学期成绩查询完成!")
break
if (counter > 0):
getScore()
print(scorenum)

except subprocess.CalledProcessError:
print("网络连接不正常!请检查网络!")
except:
print("失败!可能是你没有完成教学评价!没有完成教学评价则无法查看成绩!或用户中途取消或网络故障。")
finally:
# if os.path.exists(r'' + DstDir + '\\ScoreHelper\\CheckCode.jpg'):
# os.remove(r'' + DstDir + '\\ScoreHelper\\CheckCode.jpg')
print("程序将在3秒后退出...")
time.sleep(3)


  • 当教务网关闭以及断网的时候本程序就不灵了。。。

  • 由于session有时间限制,所以每隔一段时间(约27小时)就要重新登录

  • 最后一次发送成功 截图:

  • 2018-09-25新增——自动获取本学期必修课总数

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏