• 为了保证你在浏览本网站时有着更好的体验,建议使用类似Chrome、Firefox之类的浏览器~~
    • 如果你喜欢本站的内容何不Ctrl+D收藏一下呢,与大家一起分享各种编程知识~
    • 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此,生命在于折腾,何不年轻时多折腾一下

点饭提醒增加笑话功能

Python admin 3年前 (2017-02-11) 1076次浏览 0个评论 扫描二维码

直接上代码了,很简单的代码看看就好

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
from bs4 import BeautifulSoup
import requests
neihan_url ='http://neihanshequ.com'
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
headers = { 'User-Agent' : user_agent }
neihan_page=requests.get(neihan_url,headers=headers)
bs_neihan=BeautifulSoup(neihan_page.text,'lxml')
h1_content=bs_neihan.find_all('h1')
def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))
mail_host="smtp.126.com"  
mail_user="xxxxx@126.com"    
mail_pass="xxxxx" 


sender = 'boostcj@126.com'
receivers =['xxx@126.com','xxxx@126.com']
message = MIMEText(u'大家快点点饭啊!\n'+u'-----------华丽的分割线---------\n'+h1_content[0].next_sibling.string, 'plain', 'utf-8')
message['From'] = _format_addr('老司机 <%s>' % mail_user)
#message['To'] = _format_addr('小伙伴<%s>' % receivers)
message['To']=','.join([ _format_addr('小伙伴 <%s>' % x) for x in receivers ])
subject = '点饭专用'
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP() 
    #smtpObj.set_debuglevel(1)
    smtpObj.connect(mail_host, 25)  
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()
    print "success"
except smtplib.SMTPException:
    print "Error: can not send mail"

Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明点饭提醒增加笑话功能
喜欢 (0)
admin
关于作者:
互联网行业码农一枚/业余铲屎官/数码影音爱好者/二次元

您必须 登录 才能发表评论!