最近在学习爬虫,然后我就琢磨写一点既能对我有帮助又能用到学到的知识的程序,然后脑洞大开,就想到写一个用于软考报名邮件提醒的小程序(QAQ上半年木有过)
思路:
首先查看一下软考报名网站的页面:
传送门:软考报名
因为我在河南地区,现在还没有开始报名,所以我希望如果网页上出现了我所在地区的报名时间可以把报名有效时间通过邮件发送给我指定的邮箱,这样我就不用天天查看这个网页了
首先打开控制台,查看这个网页的结构,找到机构名称和报名有效时间的那个部分,然后查找一下规律
多看看就会发现我所需要的就是div class="layui-col-md5 col1"和div class="layui-col-md5 col2 timeW"这两个div块的内容,通过前者查找是否有我需要的城市,查到有的话,就把城市对应的时间提取出来
想要用Python发送邮件的首先需要获取邮箱的授权码,我这里用的是QQ邮箱举例:
登录QQ邮箱->设置->账户
如果这里还是不太明白的可以百度一下
源码:
#ruankao.py 用xpath解析网页
#!/usr/bin/python
#coding:utf-8
import os
import sys
import time
import requests
from lxml import etree
import aemail
reload(sys)
sys.setdefaultencoding('utf-8')
header={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': 'PHPSESSID=fc6pp410iipjlfe8cti8fdj6u1',
'Host': '112.74.37.81',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
def main():
a=''
url = 'http://112.74.37.81/rk/enroll/index.php/sign/welcome'
res = requests.get(url , headers = header, timeout = 20).text
html=etree.HTML(res)
b = html.xpath("//*[@class='layui-col-md5 col1' or @class='layui-col-md5 col2 timeW' ]/text()")
flag = 0
flag1 = 0
i = 1
for c in b:
if c == "天津": #这个可以填写你想要查询的那个省,以天津为例子
flag = 1
flag1 = i
break
i+=1
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if flag == 1:
aemail.emaill(b[i])
# aemail_yu.emaill(b[i])
print b[flag1-1]+" "+b[flag1]
else :
print "对不起,报名时间未到"
if __name__ == '__main__':
main()
#aemail.py 发送邮件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import time
from lxml import etree
import re
import smtplib
from email.mime.text import MIMEText
reload(sys)
sys.setdefaultencoding('utf8')
_user = "xxxxxx@qq.com" #发送提醒邮件的信箱
_pwd = "xxxxxxxxxxxx" #接收提醒邮件的信箱
_to = "xxxxxx@qq.com" #_user邮箱的授权码
def emaill(timee):
msg = MIMEText("xx省软考已经开始报名啦~,报名有效时间是:"+timee+" \n请在以下网站及时报名:http://112.74.37.81/rk/enroll/index.php/sign/welcome ") #发送邮件的内容
msg["Subject"] = "软考提醒——该报名啦" #发送邮件的标题
msg["From"] = _user
msg["To"] = _to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
except smtplib.SMTPException,e:
print ("Falied,%s" %e)
建议去掉注释运行~
祝下半年考软考的同学们一次通过哦~