用Python獲取智慧校園每日課表并自動(dòng)發(fā)送至郵箱
一、準(zhǔn)備工作
1.1 觀察登陸界面

我很很容易發(fā)現(xiàn),當(dāng)我們輸入了賬號(hào)與密碼之后,就會(huì)出現(xiàn)這么一個(gè)鏈接,Response返回的是false:
http://sso.cqcet.edu.cn/verificationCode?userCode={username}
此處的username指的是你的智慧校園賬號(hào);false表示登入時(shí)不需要輸入驗(yàn)證碼;當(dāng)Response返回的是true時(shí),需要輸入驗(yàn)證碼。
1.2 觀察登陸請(qǐng)求過(guò)程

我們可以發(fā)現(xiàn),Request URL為http://sso.cqcet.edu.cn/uaa/login_process
其攜帶的表單數(shù)據(jù)為:

1.3 觀察訪問(wèn)課表的url請(qǐng)求
找到課表所在,點(diǎn)擊它,你會(huì)發(fā)現(xiàn)其Request URL為http://ossc.cqcet.edu.cn/api/schedule/query。你可以在Preview發(fā)現(xiàn)課表信息。

但直接雙擊打開(kāi)此鏈接會(huì)報(bào)如下錯(cuò)誤:

原因:訪問(wèn)此鏈接需要攜帶請(qǐng)求負(fù)載

Date表示查詢的具體某天的課表
二、代碼實(shí)現(xiàn)
2.1 安裝相應(yīng)的依賴庫(kù)
鍵盤按下Win+R調(diào)出如下界面:

輸入cmd,進(jìn)入如下界面:

輸入pip install 庫(kù)名,即可安裝。
2.2 導(dǎo)入相應(yīng)的依賴庫(kù)
import json import requests import uuid # 用于獲取驗(yàn)證碼的,暫時(shí)可以不看 import yagmail # 用于發(fā)送郵件 import random import datetime #用于獲取 from fake_useragent import UserAgent # 用于獲取隨機(jī)的UserAgent
2.3 一些賬號(hào)密碼的寫入
username = '********' # 賬號(hào)
passwd = '*********' # 密碼
now_time = datetime.datetime.now ().strftime('%Y-%m-%d') # 當(dāng)天日期
contents = '' # 后面用于存儲(chǔ)課表內(nèi)容
# 隨機(jī)生成uuid
uuid = uuid.uuid4()
# 隨機(jī)UA
def get_random_ua():
ua = UserAgent()
return ua.random
2.4 url匯總及其他準(zhǔn)備
# url匯總
code_url = f'http://sso.cqcet.edu.cn/validata/code/{uuid}' # 驗(yàn)證碼獲取url
url = f'http://sso.cqcet.edu.cn/verificationCode?userCode={username}' # 判斷是否需要輸入驗(yàn)證碼url
login_url = 'http://sso.cqcet.edu.cn/uaa/login_process' # 登陸url
info_url = 'http://ossc.cqcet.edu.cn/getUserInfo' # 獲取個(gè)人信息url
schedule_url = 'http://ossc.cqcet.edu.cn/api/schedule/query' # 課表url
# 模擬登陸
headers = {
'User-Agent': get_random_ua()
}
session = requests.session()
# 驗(yàn)證碼圖片下載
def getImgCode():
img_name = str(uuid) + '.jpg'
img_data = requests.get(url=code_url, headers=headers).content
with open(img_name, 'wb') as fp:
fp.write(img_data)
print('驗(yàn)證碼獲取成功,請(qǐng)手動(dòng)識(shí)別!')
2.5 具體過(guò)程
# 驗(yàn)證碼圖片下載
def getImgCode():
img_name = str(uuid) + '.jpg'
img_data = requests.get(url=code_url, headers=headers).content
with open(img_name, 'wb') as fp:
fp.write(img_data)
print('驗(yàn)證碼獲取成功,請(qǐng)手動(dòng)識(shí)別!')
# 判斷是否需要輸入驗(yàn)證碼
isCode = session.get(url=url, headers=headers).text
if isCode == 'true':
getImgCode()
# 獲取img_code
img_code = input('請(qǐng)輸入驗(yàn)證碼:')
else:
img_code = ''
data = {
'type': '1',
'deviceId': str(uuid),
'username': username,
'password': passwd,
'img_code': img_code
}
response = session.post(url=login_url, data=data)
login_text = response.text
# print(response.status_code) # 返回200表示模擬登陸成功了!
# 個(gè)人信息獲取
info = session.get(url=info_url).text
info_data = json.loads(info) # 將json字符串轉(zhuǎn)為字典
# print(info_data)
# pprint.pprint(info_data)
contents += '親愛(ài)的' + info_data['user']['name'] + '同學(xué):\n'
# 課表獲取
schedule_json = {
'endDate': now_time,
'limit': '10',
'page': '1',
'startDate': now_time,
'type': '0'
}
schedule = session.post(url=schedule_url, json=schedule_json).text
schedule_data = json.loads(schedule) # 將json字符串轉(zhuǎn)為字典
# pprint.pprint(len(schedule_data['data'])) # 獲取數(shù)組長(zhǎng)度
# 判斷是否有課
if len(schedule_data['data']) == 0:
re_list = ['本來(lái)無(wú)一物,何處惹塵埃~', '今天沒(méi)課,放松一下吧!', '小主,今日無(wú)課~\n可想好游玩之地?', '今日無(wú)課,無(wú)需早起~']
contents += ' ' + re_list[random.randint(0, 3)]
else:
contents += ' 今日課表請(qǐng)查收~\n<hr/><table border="1" cellpadding="2" ' \
'cellspacing="0"><tr><th>上課時(shí)間</th><th>課程名稱</th><th>授課教師</th><th>上課地點(diǎn)</th></tr>'
for course_table in schedule_data['data']:
# print(course_table)
time = '<tr><td>' + course_table['date'] + ' ' + course_table['time'] + '</td>'
title = '<td>' + course_table['activity']['title'] + '</td>'
teacher = '<td>' + course_table['activity']['remarks'] + '</td>'
classroom = '<td>' + course_table['activity']['address'] + '</td></tr>'
contents += time + title + teacher + classroom
contents += '</table>'
2.6 將獲得的課表信息用郵件的方式發(fā)給自己
# 發(fā)送郵件函數(shù) def to_mail(content): yag = yagmail.SMTP( user='sm*****e@qq.com', host='smtp.qq.com', password="pdxj**********", # 注意此處的密碼不是你的郵箱密碼,具體是什么??梢匀グ俣纫幌聐agmail的使用。 smtp_ssl=True) yag.send(to=['3000004806@qq.com','s*****oe@qq.com'], subject='小主,你的課表來(lái)啦~', contents=contents) to_mail(contents) # 運(yùn)行郵件發(fā)送函數(shù)
三、效果展示

到此這篇關(guān)于用Python獲取智慧校園每日課表并自動(dòng)發(fā)送至郵箱的文章就介紹到這了,更多相關(guān)Python獲取課表后發(fā)送至郵箱內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信