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