Based on a Python crawler and sending its results to the mailbox using the SMTP protocol: A piece of the day.

Source: Internet
Author: User
Tags getmessage

The SMTP (Simple Mail Transfer Protocol) is simply the message Transfer Protocol, which is a set of rules for sending mail from the source address to the destination, and it controls the way the letters are relayed. The SMTP protocol is a TCP/IP protocol cluster that helps each computer find its next destination when sending or relaying letters. Through the server specified by the SMTP protocol, e-Mail can be sent to the recipient's server, the whole process only a few minutes. An SMTP server is a sending mail server that follows the SMTP protocol and is used to send or relay e-mail messages.

Python has a concise and usable SMTP module, which facilitates the delivery of mail, contacting the previous Python-written crawler to crawl through the satin Web (source blog: http://www.cnblogs.com/fuzzer/p/5094568.html), We can send content that it crawls down as the content of the message.

The source code is as follows:

Import sysimport reimport urllibimport smtplibimport randomfrom email.mime.text import mimetext to=[' [email  Protected] host= "smtp.163.com" #smtp服务器user = "duem123321" #用户名password = "******" #密码 postfix= "163.com" #后缀def getht ml (URL): Page=urllib.urlopen (URL) html=page.read () return htmldef getmessage (HTML): P=re.compile (R ' <div CLA ss= "Content" > (. *) </div><script type= "Text/javascript" > ") #对段子内容进行正则匹配 message=re.findall (p,html) # Returns the result of a regular match return message def send_mail (to_list,sub,content): me= "Satin concentration camp" + "<" +user+ "@" +postfix+ ">" msg = Mimetext (content,_subtype= ' plain ', _charset= ' gb2312 ') msg[' Subject '] = Sub msg[' from '] = Me msg[' to '] = ";" . Join (to_list) Try:server = Smtplib. SMTP () server.connect (host) Server.login (User,password) Server.sendmail (Me, to_list, msg.as_str         ING ()) server.close () return True except Exception, E:print str (e) return False If __name__ = = ' __main__ ': i=random.randint (1,500) i=str (i) web=gethtml (' http://ishuo.cn/subject/' +i) #该网站段子的链接特点 message=getmessage (web) message2= ". Join (message) #将结果转换为字符串类型 #message2 =message2.decode (' u      Tf8 ') message2=str (message2) Print Message2 if Send_mail (to, "a funny joke every day", Message2): print "suceed!" Else:print "failed!"

The way to choose encode: Simplified Chinese: gb2312 Traditional Chinese: big5 unicode:utf-8

The results of the receiving letter are as follows:

You can upload the code to the purchased server, modify its run time, set the recipient list, and bring a little joy to those who want to bring joy to the day.

Based on a Python crawler and sending its results to the mailbox using the SMTP protocol: A piece of the day.

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.