Python 2.7_发送简书关注的专题作者最新一篇文章及连接到邮件_20161218
2016-12-18 11:00
591 查看
最近看简书文章关注了几个专题作者,写的文章都不错,对爬虫和数据分析都写的挺好,因此想到能不能获取最新的文章推送到Ipad网易邮箱大师。邮件发送代码封装成一个函数,从廖雪峰大神那里学的
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000
网页源码获取和解析获取文章标题和url依然是用到的requests和BeautifulSoup4 模块 也封装成一个函数。
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000
网页源码获取和解析获取文章标题和url依然是用到的requests和BeautifulSoup4 模块 也封装成一个函数。
#coding: utf-8 import sys import requests from bs4 import BeautifulSoup as bs import smtplib import datetime from email.mime.text import MIMEText from email.header import Header from email.utils import parseaddr, formataddr #系统编码置为'utf-8' reload(sys) sys.setdefaultencoding('utf-8') #简书作者专题入口 rooturl='http://www.jianshu.com/notebooks/4204686/latest' #获取网页源代码 并用bs4解析 返回soup对象 def gethtml(url): res=requests.get(url) res.encoding='utf-8' html=res.text soup=bs(html,'html.parser') return soup #调用gethtml()函数获取网页soup对象 对网页进行解析 参数都是url def sendmail(url): soup=gethtml(url) catename = soup.select('h3.title a')[0].text titlename = soup.select('h4.title a')[0].text titleurl = 'http://www.jianshu.com' + soup.select('h4.title a')[0]['href'] sender = '发件人邮箱' receiver = '收件人邮箱' subject = 'python email test' smtpserver = '发件人邮箱smtp服务器' username = '发件人邮箱' password = '发件人邮箱密码' date=datetime.date.today() def _format_addr(s): name, addr = parseaddr(s) return formataddr(( Header(name, 'utf-8').encode(), addr.encode('utf-8') if isinstance(addr, unicode) else addr)) msg = MIMEText(''' 早安! Mr_Cxy,今天是%s! 简书作者"向右奔跑"在【%s】目录下最新发表的文章是: %s. 文章链接:%s ''' % (date,catename,titlename,titleurl), 'plain', 'utf-8') msg['From'] = _format_addr(u'简书App <%s>' % sender) msg['To'] = _format_addr(u'yourself~ <%s>' % receiver) msg['Subject'] = Header(u'简书作者最新文章', 'utf-8').encode() smtp = smtplib.SMTP('smtp.163.com',25) smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() print sendmail(rooturl)
相关文章推荐
- python2.7 爬取简书30日热门专题文章之简单分析_20170207
- Python之道--Python连接MYSQL数据库和发送邮件
- python 专题八 SMTP 邮件发送
- python 2.7 利用smtplib发送、抄送邮件以及发送html表格
- 【原创分享】python获取乌云最新提交的漏洞,邮件发送
- 最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件
- 软件授权码Python之道--Python连接MYSQL数据库和发送邮件
- 在django1.2+python2.7环境中使用send_mail发送邮件 推荐
- Python 通过连接qq邮箱服务器发送邮件
- Python 连接163 SMTP发送邮件
- python 自动发送邮件--不关注目标文件夹下的附件名称与数量
- linux定时监控网络连接,利用python发送邮件
- python2.7发送邮件
- python连接oracel数据库,提取数据后制图并通过邮件发送
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件 (本脚本是python的2.7.x环境下可用)
- python连接oracle,把计算结果存入本地excel并发送带附件邮件
- python3.4.3 连接Oracle生成报表并发送邮件
- Python2.7 smtplib发送带附件邮件报错STARTTLS解决方法 推荐
- 查询登录日志中,某个人最新的一 条数据,或者作者发的最新的一篇文章,group by中最新的一条数据
- python2.7 TK界面邮件发送(支持中文)