Python 2.7_ sends a brief book of attention to the author of the latest article and connected to the mail _20161218

Source: Internet
Author: User

recently read Jane book articles focused on a number of topic authors, written articles are good, the crawler and data analysis are written very well, so thought can get the latest article pushed to the ipad NetEase mailbox master. the message sending code is encapsulated into a function, learned from the great God of the Xuefeng

Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386832745198026a685614e7462fb57dbf733cc9f3ad000

Web page source code acquisition and parsing gets the article title and URL are still used in the requests and BEAUTIFULSOUP4 modules are also encapsulated into a function.

#coding: Utf-8import sysimport requestsfrom bs4 import beautifulsoup as Bsimport smtplibimport datetimefrom email.mime.te XT Import Mimetextfrom email.header import headerfrom email.utils import parseaddr, formataddr# system encoded as ' utf-8 ' Reload (SYS) Sys.setdefaultencoding (' utf-8 ') #简书作者专题入口rooturl = ' Http://www.jianshu.com/notebooks/4204686/latest ' #获取网页源代码 Use BS4 parsing to return soup object def gethtml (URL): Res=requests.get (URL) res.encoding= ' utf-8 ' html=res.text soup=bs (HTML, ' HTML. Parser ') return soup# call gethtml () function to get Web page Soup Object parsing parameters are urldef 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 ' + so Up.select (' H4.title a ') [0][' href '] sender = ' Sender mailbox ' receiver = ' recipient mailbox ' subject = ' python email test ' smtpserve        R = ' Sender Mailbox SMTP server ' username = ' Sender mailbox ' Password = ' sender mailbox password ' date=datetime.date.today () def _format_addr (s): Name, addr = parseaddr (s) returnFormataddr (Header (name, ' Utf-8 '). Encode (), Addr.encode (' Utf-8 ') if isinstance (addr, Unicode) Else    addr)) msg = Mimetext ("' Good morning!    Mr_cxy, today is%s!.    Book author "Run Right" the most recent article in the "%s" Directory is:%s. Article link:%s "% (date,catename,titlename,titleurl), ' Plain ', ' utf-8 ') msg[' from '] = _format_addr (U ' simplified book app <%s&gt ; '% sender ') msg[' to '] = _format_addr (U ' yourself~ <%s> '% receiver) msg[' Subject '] = Header (U ' Jane book author latest article ', ' utf- 8 '). Encode () SMTP = Smtplib. SMTP (' smtp.163.com ', +) Smtp.login (username, password) smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit () print sendmail (Rooturl)

  

Python 2.7_ sends a brief book of attention to the author of the latest article and connected to the mail _20161218

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.