未分类

一个小脚本:检测是否有新的事业编信息,并发送邮件提示

2018年1月23日

一个朋友,让我给他看看我们这里的事业编信息,我就想给他写个脚本,自己检测是否有新的招聘信息,并发送邮件通知。思路很简单,那用python做吧

[code lang=”python”]
# -*- coding:utf-8 -*-
”’
检查是否有新的潍坊公务员招聘
”’
import os
import urllib
import urllib2
import re
from lxml import etree
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8′)

”’
发送邮件
”’
def sendEmail(article):
config = {
‘address’: ‘address’,
‘port’: 994,
‘user’: ‘xx@xx.xx’,
‘password’: ‘password’
}
sender = {
‘nickname’: ‘xx’
}
receiver = {
’email’: ‘xx’,
‘nickname’: ‘xx’
}
otherReceiver = [‘xx’]
htmlContent = """
<h1>有一个新的事业编招聘信息!!!</h1>
<h2><a href="__href__" target="_blank" rel="noopener">__title__</a></h2>
""".replace(‘__href__’, article[‘href’]).replace(‘__title__’, article[‘title’])
print htmlContent

ret = True
try:

msg = MIMEText(htmlContent, ‘html’, ‘utf-8’)

msg[‘From’] = formataddr(["xx", config[‘user’]]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg[‘To’] = formataddr([receiver[‘nickname’], receiver[’email’]])

msg[‘Cc’] = ";".join(otherReceiver)
msg[‘Subject’] = "#潍坊事业编招聘#发布了一篇新文章《__title__》".replace(‘__title__’, article[‘title’]) # 邮件的主题,也可以说是标题

server = smtplib.SMTP_SSL(config[‘address’], config[‘port’]) # 发件人邮箱中的SMTP服务器,端口是25
# print server.connect()

print server.login(config[‘user’], config[‘password’]) # 括号中对应的是发件人邮箱账号、邮箱密码
# 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
print server.sendmail(config[‘user’], [receiver[’email’]] + otherReceiver, msg.as_string())
server.quit() # 关闭连接

except Exception, e: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
print e
ret=False

return ret
”’
判断是否网站有更新
”’
def isHasNewArticle(article):
fileName = ‘article.txt’
if False == os.path.exists(fileName):
# os.mknod(fileName)
os.system("touch " + fileName)

try:
f = open(fileName, ‘r+’)

title = f.readline()

if article[‘title’] == title:
f.close()
return False

f.write(article[‘title’])
f.close()
return True

except Exception, e:
print e
exit(0);
pass

”’
main
”’
url = "http://www.wfrsks.com/Home/List/115"
user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)’
headers = {‘User-Agent’: user_agent}
try:
request = urllib2.Request(url=url, headers=headers)
response = urllib2.urlopen(request)
responseText = response.read().decode(‘utf-8’)

selector = etree.HTML(responseText)
something = selector.xpath(‘//div[@class="listcont"]/ul/li[1]’)

li = something[0]
a = li.xpath(‘a’)[0]

article = {
‘title’: str(a.xpath(‘@title’)[0]),
‘href’: ‘http://www.wfrsks.com’ + str(a.xpath(‘@href’)[0]),
‘publish_time’: li.xpath(‘span/text()’)[0]
}
print article
if isHasNewArticle(article):
sendEmail(article)

except urllib2.URLError, e:
if hasattr(e, "code"):
print e.code
if hasattr(e, "reason"):
print e.reason
[/code]

然后用crontab做定时任务

[code lang=”shell”]
* */1 * * * /usr/bin/python /data/xx.py >> /tmp/crontab.log 2&>1
[/code]

发送邮件的时候有个坑:阿里云的服务器把25端口限制了,发送邮件总是超时,最后加上了ssl。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注