【Python】一个脚本检测期末考试是否出分
本帖最后由 WoShiXXX 于 2021-1-5 09:40 编辑楼主期末考的太好,但是分数一直都没有出,楼主等不及了,于是乎,就有了下面的脚本(PS:该脚本支持自动发送邮件,改一改还能支持GitHub Actions),第一次写爬虫,如有不足请各位大神指出
# coding=utf-8
import requests #用于发送http请求
import os #用于获取环境变量
import time #用于生成url中的n
import json #用于解析json
import smtplib #用于发送邮件
import math #用于向下取整
from email.mime.text import MIMEText
basepayload = '?JSONPARAM=%7B%22role_id%22:%22role_2%22,%22user_id%22:%22xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%22,%22exbatch_name%22:%22%22,%22page_size%22:4,%22page_num%22:1,%22student_id%22:%22xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%22%7D' #需要到浏览器中复制、并且要复制转义后的字符串
headers = {
'Host': 'xf.zyzhedu.com',
'Connection': 'keep-alive',
'sec-ch-ua': '%22Google%20Chrome%22%3Bv%3D%2287%22%2C%20%22%20Not%3BA%20Brand%22%3Bv%3D%2299%22%2C%20%22Chromium%22%3Bv%3D%2287%22',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'X-Requested-With': 'XMLHttpRequest',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://xf.zyzhedu.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9'
} #配置请求头用于反爬
unixtime = time.time()
payload_ = math.floor(unixtime)
print(payload_)
payload_n = math.floor(unixtime / 1000)
payload = basepayload + '&_n=' + str(payload_) + '&_=' + str(payload_n)
url = 'https://xf.zyzhedu.com/XFFurtherEducation/examinationBatchRoute/getAchExamBatchList' + payload
print(url)
res = requests.get(url,headers = headers)
print(res.status_code)
res_dict = json.loads(res.text)
print(res_dict['resultdata']['dataNum']) #输出考试个数
# 开始配置邮件信息
mail_host = 'smtp.yeah.net' #smtp地址
mail_user = '邮箱用户名'
mail_pass = '邮箱密码'
sender = '邮件发送者地址'
receivers = ['接收者1' , '接收者2']
# 结束配置邮件信息
# 开始设置邮件内容
message = MIMEText('出分了,可以开始查分','html','UTF-8')
message['Subject'] = '出分了'
message['From'] = 'WoShiXXX <' + sender + '>'
message['To'] = 'zyh <' + receivers + '>'
message['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
# 结束设置邮件内容
if res_dict['resultdata']['dataNum'] == 3:
try:
smtpObj = smtplib.SMTP()
#连接到服务器
smtpObj.connect('smtp.yeah.net',25)
#登录到服务器
smtpObj.login(mail_user,mail_pass)
#发送
message_str = message.as_string()
print(message)
print(message_str)
smtpObj.sendmail(sender,receivers, message_str)
message['To'] = 'jieshouren <' + receivers + '>'
message['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
message_str = message.as_string()
smtpObj.sendmail(sender,receivers, message_str)
#退出
smtpObj.quit()
print('success')
except smtplib.SMTPException as e:
print('error',e) #打印错误
# 结束发送邮件
nizsm123 发表于 2021-1-4 10:51
%22Google%20Chrome%22%3Bv%3D%2287%22%2C%20%22%20Not%3BA%20Brand%22%3Bv%3D%2299%22%2C%20%22Chromium%2 ...
源字符串为:"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
因为有双引号,为了避免发送请求时出现歧义,我用escape转义了一下 %22Google%20Chrome%22%3Bv%3D%2287%22%2C%20%22%20Not%3BA%20Brand%22%3Bv%3D%2299%22%2C%20%22Chromium%22%3Bv%3D%2287%22
这一串咋搞的?
WoShiXXX 发表于 2021-1-4 10:56
源字符串为:"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
因为有双引号,为了避 ...
谢谢解答!!!!!!! 好凡尔赛 内网的能不能 知轩 发表于 2021-1-4 14:19
内网的能不能
得看是什么系统,我这个适用于北京中育开发的查分系统,可以自己分析API,然后自己写爬虫 可以改一改用sever酱微信推送更好一些 唯唯子 发表于 2021-1-4 14:37
可以改一改用sever酱微信推送更好一些
我在程序里设置的接收邮箱是qq邮箱,那个qq邮箱还绑定我微信了,所以应该跟server酱没什么区别 WoShiXXX 发表于 2021-1-4 15:03
我在程序里设置的接收邮箱是qq邮箱,那个qq邮箱还绑定我微信了,所以应该跟server酱没什么区别
有些邮件会延迟推送。。。 我也不知道咋回事 遇到过很多次了
(ix 14.0.1系统)
页:
[1]
2