目錄
- 一、準備工作
- 1.1 觀察登陸界面
- 1.2 觀察登陸請求過程
- 1.3 觀察訪問課表的url請求
- 二、代碼實現(xiàn)
- 2.1 安裝相應(yīng)的依賴庫
- 2.2 導(dǎo)入相應(yīng)的依賴庫
- 2.3 一些賬號密碼的寫入
- 2.4 url匯總及其他準備
- 2.5 具體過程
- 2.6 將獲得的課表信息用郵件的方式發(fā)給自己
- 三、效果展示
一、準備工作
1.1 觀察登陸界面

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

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

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

但直接雙擊打開此鏈接會報如下錯誤:

原因:訪問此鏈接需要攜帶請求負載

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

輸入cmd
,進入如下界面:

輸入pip install 庫名
,即可安裝。
2.2 導(dǎo)入相應(yīng)的依賴庫
import json
import requests
import uuid # 用于獲取驗證碼的,暫時可以不看
import yagmail # 用于發(fā)送郵件
import random
import datetime #用于獲取
from fake_useragent import UserAgent # 用于獲取隨機的UserAgent
2.3 一些賬號密碼的寫入
username = '********' # 賬號
passwd = '*********' # 密碼
now_time = datetime.datetime.now ().strftime('%Y-%m-%d') # 當天日期
contents = '' # 后面用于存儲課表內(nèi)容
# 隨機生成uuid
uuid = uuid.uuid4()
# 隨機UA
def get_random_ua():
ua = UserAgent()
return ua.random
2.4 url匯總及其他準備
# url匯總
code_url = f'http://sso.cqcet.edu.cn/validata/code/{uuid}' # 驗證碼獲取url
url = f'http://sso.cqcet.edu.cn/verificationCode?userCode={username}' # 判斷是否需要輸入驗證碼url
login_url = 'http://sso.cqcet.edu.cn/uaa/login_process' # 登陸url
info_url = 'http://ossc.cqcet.edu.cn/getUserInfo' # 獲取個人信息url
schedule_url = 'http://ossc.cqcet.edu.cn/api/schedule/query' # 課表url
# 模擬登陸
headers = {
'User-Agent': get_random_ua()
}
session = requests.session()
# 驗證碼圖片下載
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('驗證碼獲取成功,請手動識別!')
2.5 具體過程
# 驗證碼圖片下載
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('驗證碼獲取成功,請手動識別!')
# 判斷是否需要輸入驗證碼
isCode = session.get(url=url, headers=headers).text
if isCode == 'true':
getImgCode()
# 獲取img_code
img_code = input('請輸入驗證碼:')
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表示模擬登陸成功了!
# 個人信息獲取
info = session.get(url=info_url).text
info_data = json.loads(info) # 將json字符串轉(zhuǎn)為字典
# print(info_data)
# pprint.pprint(info_data)
contents += '親愛的' + 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ù)組長度
# 判斷是否有課
if len(schedule_data['data']) == 0:
re_list = ['本來無一物,何處惹塵埃~', '今天沒課,放松一下吧!', '小主,今日無課~\n可想好游玩之地?', '今日無課,無需早起~']
contents += 'nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;' + re_list[random.randint(0, 3)]
else:
contents += 'nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;今日課表請查收~\nhr/>table border="1" cellpadding="2" ' \
'cellspacing="0">tr>th>上課時間/th>th>課程名稱/th>th>授課教師/th>th>上課地點/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**********", # 注意此處的密碼不是你的郵箱密碼,具體是什么。可以去百度一下yagmail的使用。
smtp_ssl=True)
yag.send(to=['3000004806@qq.com','s*****oe@qq.com'],
subject='小主,你的課表來啦~',
contents=contents)
to_mail(contents) # 運行郵件發(fā)送函數(shù)
三、效果展示

到此這篇關(guān)于用Python獲取智慧校園每日課表并自動發(fā)送至郵箱的文章就介紹到這了,更多相關(guān)Python獲取課表后發(fā)送至郵箱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 教你怎么用python selenium實現(xiàn)自動化測試
- 使用Gitee自動化部署python腳本的詳細過程
- Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP
- python辦公自動化之excel的操作
- 教你利用Selenium+python自動化來解決pip使用異常
- 十個Python自動化常用操作,即拿即用
- python自動化之如何利用allure生成測試報告
- Python 制作自動化翻譯工具
- 使用Python自動化Microsoft Excel和Word的操作方法
- python+requests+pytest接口自動化的實現(xiàn)示例