欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

用Python查成績的方法

小編給大家分享一下用Python查成績的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)公司主營西安網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),西安h5微信小程序開發(fā)搭建,西安網(wǎng)站營銷推廣歡迎西安等地區(qū)企業(yè)咨詢

怎么用Python查成績

用Python查成績可以使用requests庫,模擬登錄教務(wù)系統(tǒng),然后通過正則查詢成績信息即可。

設(shè)計(jì)思路:

設(shè)計(jì)思路很簡單,首先對已有的成績進(jìn)行處理,變?yōu)閘ist集合,然后定時(shí)爬取教務(wù)系統(tǒng)查成績的頁面,對爬取的成績也處理成list集合,如果newList的長度增加了,就找出增加的部分,并通過郵件通知我。

推薦學(xué)習(xí)《Python教程》。

腳本運(yùn)行效果:

用Python查成績的方法

發(fā)送郵件通知:

用Python查成績的方法

代碼如下:

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
    #但我們學(xué)校后臺將用戶名和密碼進(jìn)行了加密
    #通過觀察瀏覽器的請求數(shù)據(jù)跟頁面源碼猜出學(xué)校后臺的加密方式
    data={
        #出于學(xué)校安全考慮,這里就不給出加密方式了
        'encoded':'xxxxxxxxxxxxxxxxxxx'
    }
    session = requests.Session()
    session.post('http://jwc.sgu.edu.cn/jsxsd/xk/LoginToXk',data=data)
    #請求2019-2020-1學(xué)期的所有成績
    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)
    #對爬回來數(shù)據(jù)進(jìn)行封裝
    soup = BeautifulSoup(r.text, 'html.parser')
    #返回已有的成績列表
    oldList = toList(soup)
    max = len(oldList)
    #這里用死循環(huán)定時(shí)爬取成績頁面分析是否分布新成績
    while (True):
        #post跟get方式不能亂用,不然數(shù)據(jù)會出錯(cuò)
        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)
                #獲取兩個(gè)列表不同之處,不同的就是新成績
                diflist = compareTwoList(oldList, newlist)
                oldList=newlist
                if diflist=='':
                    send("unkowned Error","unkowned Error")
                else:
                    #有新成績了,發(fā)送郵件通知我
                    send('you have new course sorce!!', diflist)
                max = length
            print('last running time was:',datetime.datetime.now())
            #定時(shí)作用,500s查一次
            time.sleep(500)
        else:
            # 發(fā)送郵件斷開連接了 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'
    # 密碼(部分郵箱為授權(quán)碼)
    mail_pass = '授權(quán)碼'
    # 郵件發(fā)送方郵箱地址
    sender = '發(fā)送方郵箱地址'
    # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
    receivers = ['yoletpig@qq.com']

    # 設(shè)置email信息
    # 郵件內(nèi)容設(shè)置
    message = MIMEText(msg, 'plain', 'utf-8')
    # 郵件主題
    message['Subject'] = Header(title,'utf-8')
    # 發(fā)送方信息
    message['From'] = sender
    # 接受方信息
    message['To'] = receivers[0]

    # 登錄并發(fā)送郵件
    try:
        # smtpObj = smtplib.SMTP()
        # # 連接到服務(wù)器
        # smtpObj.connect(mail_host, 25)
        smtpObj = smtplib.SMTP_SSL(mail_host)
        # 登錄到服務(wù)器
        smtpObj.login(mail_user, mail_pass)
        # 發(fā)送
        smtpObj.sendmail(
            sender,receivers,message.as_string())
        # 退出
        smtpObj.quit()
        print('success')
    except smtplib.SMTPException as e:
        print('error', e)  # 打印錯(cuò)誤

def toList(soup):
    flag = True
    list = []
    strs = ''
    #對tr標(biāo)簽下的td進(jìn)行遍歷并取值
    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()

看完了這篇文章,相信你對用Python查成績的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:用Python查成績的方法
文章URL:http://chinadenli.net/article14/jiiege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站、域名注冊、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)