這幾天考了大大小小幾門課,教務系統又沒有成績通知功能,為了急切想知道自己掛了多少門,于是我寫下這個腳本。
設計思路:
設計思路很簡單,首先對已有的成績進行處理,變為list集合,然后定時爬取教務系統查成績的頁面,對爬取的成績也處理成list集合,如果newList的長度增加了,就找出增加的部分,并通過郵件通知我。
腳本運行效果:
服務器:
發送郵件通知:
代碼如下:
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
|
import datetime import time from email.header import Header import requests import re import smtplib from email.mime.text import MIMEText from bs4 import BeautifulSoup def listener(): #在這里我通過模擬登陸的方式登陸 #一般來說這里填寫的是username跟password #但我們學校后臺將用戶名和密碼進行了加密 #通過觀察瀏覽器的請求數據跟頁面源碼猜出學校后臺的加密方式 data = { #出于學校安全考慮,這里就不給出加密方式了 'encoded' : 'xxxxxxxxxxxxxxxxxxx' } session = requests.Session() session.post( 'http://jwc.sgu.edu.cn/jsxsd/xk/LoginToXk' ,data = data) #請求2019-2020-1學期的所有成績 r_data = { 'kksj' : '2019-2020-1' , 'kcxz' : '', 'kcmc' : '', 'xsfs' : 'all' } r = session.post( 'http://jwc.sgu.edu.cn/jsxsd/kscj/cjcx_list' , data = r_data) #對爬回來數據進行封裝 soup = BeautifulSoup(r.text, 'html.parser' ) #返回已有的成績列表 oldList = toList(soup) max = len (oldList) #這里用死循環定時爬取成績頁面分析是否分布新成績 while ( True ): #post跟get方式不能亂用,不然數據會出錯 r = session.post( 'http://jwc.sgu.edu.cn/jsxsd/kscj/cjcx_list' ,data = r_data) soup = BeautifulSoup(r.text, 'lxml' ) #print(soup.prettify()) length = len (soup.find_all(string = re. compile ( '2019-2020-1' ))) - 1 print ( "course_length: " ,length) if (r.status_code = = 200 and length ! = 0 ): if (length > max ): #查詢新出的成績列表 newlist = toList(soup) #獲取兩個列表不同之處,不同的就是新成績 diflist = compareTwoList(oldList, newlist) oldList = newlist if diflist = = '': send( "unkowned Error" , "unkowned Error" ) else : #有新成績了,發送郵件通知我 send( 'you have new course sorce!!' , diflist) max = length print ( 'last running time was:' ,datetime.datetime.now()) #定時作用,500s查一次 time.sleep( 500 ) else : # 發送郵件斷開連接了 print("had disconnected...") send( "your server is disconnected!!!" , "your server is disconnected!!!" ) break def send(title,msg): mail_host = 'smtp.qq.com' # 你的qq郵箱名,沒有.com mail_user = '你的qq郵箱名,沒有.com' # 密碼(部分郵箱為授權碼) mail_pass = '授權碼' # 郵件發送方郵箱地址 sender = '發送方郵箱地址' # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個郵件地址群發 receivers = [ 'yoletpig@qq.com' ] # 設置email信息 # 郵件內容設置 message = MIMEText(msg, 'plain' , 'utf-8' ) # 郵件主題 message[ 'Subject' ] = Header(title, 'utf-8' ) # 發送方信息 message[ 'From' ] = sender # 接受方信息 message[ 'To' ] = receivers[ 0 ] # 登錄并發送郵件 try : # smtpObj = smtplib.SMTP() # # 連接到服務器 # smtpObj.connect(mail_host, 25) smtpObj = smtplib.SMTP_SSL(mail_host) # 登錄到服務器 smtpObj.login(mail_user, mail_pass) # 發送 smtpObj.sendmail( sender,receivers,message.as_string()) # 退出 smtpObj.quit() print ( 'success' ) except smtplib.SMTPException as e: print ( 'error' , e) # 打印錯誤 def toList(soup): flag = True list = [] strs = '' #對tr標簽下的td進行遍歷并取值 for tr in soup.find_all( 'tr' ): if flag: flag = False ; continue i = 1 for td in tr.stripped_strings: if (i = = 1 or i = = 2 ): i + = 1 continue strs + = "_" + td i + = 1 list .append(strs) strs = '' return list def compareTwoList(oldList,newList): diflist = '' for sub in newList: #判斷是否唯一 if (oldList.count(sub) = = 0 ): diflist = sub break return diflist if __name__ = = '__main__' : listener() |
這個腳本不出意外的話要運行到我所有成績出來為止,但我電腦肯定不會這么多天不關機呀,于是我就將這個腳本放到服務器上運行,如何在ubuntu下運行python文件,大家可以看一下我的另一篇文章:
yoletPig的博客:Ubuntu下運行python文件
總結
以上所述是小編給大家介紹的python爬蟲爬取監控教務系統的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
原文鏈接:https://blog.csdn.net/qq_42840203/article/details/103846093