表弟高考结束,查询录取结果成为最痛苦的事情,每天都要登录安徽教育招生考试网去查询有没有出结果。为此我在我写的2019年的考试院查询接口分析 的基础上添加了邮件功能,即代码自动查询,直到发现录取结果后发送邮件到指定邮箱。
原理很简单,这里就直接给出代码了。有一点需要注意:如果要用代码发送邮件需要前往你的邮件提供商进行相关设置!
gklqxx.py
#录取查询
#Michael Jiang(BH6AOL)
#v0.0.1 2019年7月29日18:34:03
#v0.0.2 2021年7月23日10:24:00
import requests
import time
from bs4 import BeautifulSoup
import sendEmail
in_url = "http://cx.ahzsks.cn/pugao/pglq2021_in-00b8bd3f-d5c8-4f90-b4e7-1b76cf147bbd.php"
out_url = "http://cx.ahzsks.cn/pugao/pglq2021_out-00b8bd3f-d5c8-4f90-b4e7-1b76cf147bbd.php"
#考生号
ksh = "************"
#身份证号
sfzh = "*************"
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE',
'Cookie':'PHPSESSID=duapea8pnci4jku31sjeeinse7'
}
need_send_email = True
def update():
global need_send_email
r = requests.get(in_url,headers = headers)
r.encoding="utf8"
html = BeautifulSoup(r.text, "html.parser")
attr = html.find("input", attrs={"id":"yzm"})
yzm = attr.string
print("验证码:"+yzm)
data = {
"ksh":ksh,
"sfzh":sfzh,
"yzm":yzm
}
r = requests.post(out_url,headers = headers,data = data)
r.encoding="utf8"
html = BeautifulSoup(r.text, "html.parser")
attr = html.find_all('td')
# print(len(attr)) 如果长度大于2 则发送email 到592348268@qq.com
# 如果有录取结果则存结果字符串
out = ""
if attr[1].text != "暂无录取信息" :
for line in attr:
out += line.get_text().strip()
# 执行一次发送邮件任务
if need_send_email:
sendEmail.send_email(out)
need_send_email = False
for line in attr:
print(line.get_text().strip())
if __name__ == "__main__":
while(True):
try:
print(time.ctime())
update()
print()
time.sleep(60)
except Exception as e:
print(e)
time.sleep(60)
pass
sendEmail.py
#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
from time import sleep
from email.header import Header
email_reciver = ['********@qq.com','**********@qq.com']
def send_email(result):
smtpserver='smtp.office365.com'
username='******@outlook.com'
password='*******'
from_addr='**********@outlook.com'
message = MIMEText(result, 'plain', 'utf-8')
message['Subject'] = Header("录取结果", 'utf-8')
message['From']=from_addr
sm=smtplib.SMTP(smtpserver,port=587,timeout=20)
sm.set_debuglevel(1)
sm.ehlo()
sm.starttls()
sm.ehlo()
sm.login(username,password)
for recv in email_reciver:
to_addr= recv
message['To']=to_addr
msg=message.as_string()
sm.sendmail(from_addr,to_addr,msg)
sleep(3)
sm.quit()
运行结果