# -*- coding:utf-8 -*-
import requests
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from lxml import etree
#不得姐
url = "http://www.budejie.com/text/"
page = requests.get(url)
html = page.text
selector = etree.HTML(html)
# 第三方 SMTP 服务
mail_host="smtp.163.com" #设置服务器
mail_user="########" #用户名
mail_pass="######" #口令
#邮箱
sender = '########@163.com'
receivers = ['#########@qq.com']
#头像
uimg = selector.xpath('//div[@class="u-img"]/a/img/@src')
#昵称
uname = selector.xpath('//a[@class="u-user-name"]/text()')
#内容
ucontent = selector.xpath('//div[@class="j-r-list-c-desc"]/a/text()')
#发邮件
message = MIMEText('不得姐更新提示', 'plain', 'utf-8')
message['From'] = Header("不得姐", 'utf-8')
message['To'] = Header("我的APP", 'utf-8')
subject = '不得姐更新'
message['Subject'] = Header(subject, 'utf-8')
#一旦从不得姐获取不到信息,立即发送邮件
if not uimg or uname or ucontent:
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, '#########@163.com', message.as_string()) ##这里本来应该填写receviers,但是发现发送失败,可能是因为QQ邮箱的smtp没有启动。所以还是让邮箱自己发给自己吧。
except smtplib.SMTPException:
print "error"