自动检索学校列表里有没有指定学校,如果有的话则自动查询成绩,并将结果发送到指定邮箱。
有个小BUG就是最后邮件会发两次。
基于Python2.7,Python3的urllib可能用法有所不同。
先检索是否有指定学校:
#构造请求url,需要用地区编号+时间戳,地区编号可以在浏览器F12中查看,例如北京是11 url='https://yz.chsi.com.cn/apply/code/cjcxdw.do?ssdm=11&ts='+str(int(round(time.time() * 1000))) #请求头注意Host和Referer headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host': 'yz.chsi.com.cn', 'Accept-Language': 'zh-CN,zh;q=0.9', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate, br', 'Cookie': cookie, 'Referer': 'https://yz.chsi.com.cn/apply/cjcx/', 'Pragma': 'no-cache', 'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache' } #发送请求,获取返回列表 req = urllib2.Request(url, None, headers) response = urllib2.urlopen(req) res = response.read() #格式化为json对象,获取其中的dms,即学校列表 jsonobj=json.loads(res) schoolist=jsonobj['dms'] flag=0 #遍历列表,查找是否有北航 for i in schoolist: schoolname=i['mc'] if schoolname.encode('utf-8') == '北京航空航天大学': flag=1 schoolcode=i['dm'] |
查到之后构造数据发送POST请求获取结果,转发邮件,不单列了。
全部代码
#coding=utf-8 import urllib2 import urllib import threading import time import json import smtplib import gzip import StringIO from email.mime.text import MIMEText from email.header import Header cookie='JSESSIONID=D5B7900FBCE661659978464309BD23D1; acw_tc=2760824b15502052244788631e101590a210f068338831246151b55dcc5577; JSESSIONID=5BB2C7ADAFD3F24E7179F7E0571B3606; _ga=GA1.4.129029864.1550205225; _gid=GA1.4.1924511835.1550205225' mail_host = "smtp.163.com" # 设置服务器 mail_user = "********@163.com" # 用户名 mail_pass = "*********" # 密码 sender = '*********@163.com' #发件人邮箱 receivers = ['********@163.com'] # 收件人邮箱,可以是个列表 def fetch2(url,post): headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host': 'yz.chsi.com.cn', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip,deflate', 'Cookie': cookie, 'Referer': 'https://yz.chsi.com.cn/apply/cjcx/', 'Pragma': 'no-cache', 'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control':'no-cache' } data = urllib.urlencode(post) req = urllib2.Request(url, data,headers) response = urllib2.urlopen(req) page_source = response.read() return page_source def mainfunc(): url='https://yz.chsi.com.cn/apply/code/cjcxdw.do?ssdm=11&ts='+str(int(round(time.time() * 1000))) headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host': 'yz.chsi.com.cn', 'Accept-Language': 'zh-CN,zh;q=0.9', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate, br', 'Cookie': cookie, 'Referer': 'https://yz.chsi.com.cn/apply/cjcx/', 'Pragma': 'no-cache', 'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache' } print("开始查询!") try: req = urllib2.Request(url, None, headers) response = urllib2.urlopen(req) res = response.read() jsonobj=json.loads(res) schoolist=jsonobj['dms'] flag=0 #设置默认学校代码,可以随便填 schoolcode=10050 global timer #查找是否有北航 for i in schoolist: schoolname=i['mc'] if schoolname.encode('utf-8') == '北京航空航天大学': flag=1 schoolcode=i['dm'] #查到之后构造查询参数 if(flag==1): url2 = 'https://yz.chsi.com.cn/apply/cjcx/cjcx.do' post = {'xm': '张三', 'zjhm':'身份证号', 'ksbh': '', 'bkdwdm': schoolcode, 'checkcode': ''} re = fetch2(url2, post) #解压收到的gzip网页 re = StringIO.StringIO(re) gz = gzip.GzipFile(fileobj=re) re = gz.read() gz.close() #构造邮件消息,发送HTML类型邮件 message = MIMEText(re, _subtype='html',_charset='utf-8') message['From'] = "lufer<*********@163.com>" #发件人姓名 message['To'] = "Receiver" #收件人姓名 subject = '出分了!' #邮件主题 message['Subject'] = Header(subject, 'utf-8') smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.ehlo() smtpObj.starttls() smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "已查询到成绩j,邮件发送成功" smtpObj.quit() timer.cancel() return else: print("没有查询到制定学校") except Exception as e: print('发生了异常:', e) #一分钟查询一次 timer = threading.Timer(60, mainfunc) timer.start() mainfunc() |
文章评论