Python Get Weather +email notification

Source: Internet
Author: User
Tags define function

Source of information fetching:http://www.tianqi.com/( weather net )

mail server (send): 126 mailbox

Mail server (receive): QQ mailbox


The code is as follows:

#!/usr/bin/env python# -*- coding:utf-8 -*- import smtplibimport urllib, urllib2import re# define function, send mail def sendmail (body):    smtp_server =  ' Smtp.126.com '     from_mail =  ' [email protected] '           #密码使用授权码替代, otherwise 535 authentication error will be reported     mail_pass =  ' ********* '      to_mail = [' [email protected] ']    from_name =  ' Weather monitor '     subject =  ' raining today! '     mail = [         "from: %s  <%s> " %  (from_name, from_mail),        " to:  %s '  %  ', '. Join (To_mail),         ' subject: %s '  %  subject,         "",        body         ]    msg =  ' \ n '. Join (mail)     try:         s = smtplib. Smtp_ssl (' smtp.126.com ', 465)         s.login (From_mail, mail_pass)         s.sendmail (from_mail, to_mail, msg)          s.quit ()     except smtplib. smtpexception as e:        raise e         if __name__ ==  "__main__":     headers={' User-agent ':  ' mozilla/5.0  (windows nt 6.1; wow64)  AppleWebKit/537.36  (khtml ,  like gecko)  chrome/55.0.2883.87 safari/537.36 '}         #定位城市, Shenzhen     url= ' http://www.tianqi.com/shenzhen/'     try:         request = urllib2. Request (url,headers=headers)         response =  Urllib2.urlopen (Request)         urlhtml = response.read ()         except Exception as e:         raise e         #抓取关键字正则表达式          re_page = re.compile (R ' <dd class= "name" >.*?


Operation Result:



Python Get Weather +email notification

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.