栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python获取天气+email通知

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python获取天气+email通知

信息抓取来源:http://www.tianqi.com/(天气网 )

邮件服务器(发送):126邮箱

邮件服务器(接收):QQ邮箱


代码如下:

#!/usr/bin/env python

# -*- coding:utf-8 -*- 


import smtplib

import urllib,urllib2

import re


#定义函数,发送邮件

def sendMail(body):

    smtp_server = 'smtp.126.com'

    from_mail = 'hzpzc68@126.com'

    

    #密码使用授权码替代,否则会报535等认证错误

    mail_pass = '*********'

    to_mail = ['1124657106@qq.com']

    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'}

    

    #定位城市,深圳

    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'

.*?(.*?).*?
(.*?)
.*?.*?(.*?)(.*?)',re.S)

    

    items = re_page.findall(urlhtml)

    dic = {}

    dic["城市"] = items[0][0]

    dic["日期"] = items[0][1]

    dic["天气"] = items[0][2]

    dic["温度"] = items[0][3]

    

    #判断天气是否含有“雨”关键字

    if "雨" in dic["天气"]:

        sendMail("It's rainy today. Remember to bring your umbrella!" +"n" +"城市: " +dic["城市"] +"n" +"日期: " +dic["日期"] +"n" +"天气: " +dic["天气"] +"n" +"温度: " +dic["温度"])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/223317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号