教你用python爬蟲監控教務系統,查成績快人一步!


教你用python爬蟲監控教務系統,查成績快人一步!
這幾天考了大大小小幾門課,教務系統又沒有成績通知功能,為了急切想知道自己掛了多少門,於是我寫下這個腳本。

設計思路:
設計思路很簡單,首先對已有的成績進行處理,變為list集合,然后定時爬取教務系統查成績的頁面,對爬取的成績也處理成list集合,如果newList的長度增加了,就找出增加的部分,並通過郵件通知我。

腳本運行效果:
服務器:

發送郵件通知:


代碼如下:

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()

這個腳本不出意外的話要運行到我所有成績出來為止,但我電腦肯定不會這么多天不關機呀,於是我就將這個腳本放到服務器上運行

http://mseo.chinaz.com/lvyous1.nx04.com/   http://seo.chinaz.com/lvyous2.nx04.com/     http://mseo.chinaz.com/lvyous3.nx04.com/   http://seo.chinaz.com/lvyous4.nx04.com/ 

http://mseo.chinaz.com/lvyous5.nx04.com/   http://seo.chinaz.com/lvyous6.nx04.com/     http://mseo.chinaz.com/lvyous7.nx04.com/   http://seo.chinaz.com/lvyous8.nx04.com/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM