本帖最后由 WoShiXXX 于 2021-1-5 09:40 编辑
楼主期末考的太好,但是分数一直都没有出,楼主等不及了,于是乎,就有了下面的脚本(PS:该脚本支持自动发送邮件,改一改还能支持GitHub Actions),第一次写爬虫,如有不足请各位大神指出
[Python] 纯文本查看 复制代码 # 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[0] + '>'
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[0], message_str)
message['To'] = 'jieshouren <' + receivers[1] + '>'
message['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
message_str = message.as_string()
smtpObj.sendmail(sender,receivers[0], message_str)
#退出
smtpObj.quit()
print('success')
except smtplib.SMTPException as e:
print('error',e) #打印错误
# 结束发送邮件
|