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 = '[email protected]'
    
    #密码使用授权码替代,否则会报535等认证错误
    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'}
    
    #定位城市,深圳
    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">.*?<h2>(.*?)</h2>.*?<dd class="week">(.*?)</dd>.*?<span>.*?<b>(.*?)</b>(.*?)</span>',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["温度"])

运行结果:

原文地址:http://blog.51cto.com/huangzp/2072702

时间: 2024-08-01 18:57:13

python获取天气+email通知的相关文章

python获取天气状况并以邮件的形式发到目的邮箱

python爬取天气情况 下面为示例代码: from urllib.request import urlopen from bs4 import BeautifulSoup from urllib.error import HTTPError import smtplib from email.mime.text import MIMEText from email.utils import formataddr import time """爬虫程序是一个需要后期投入很大维

python获取天气以及地理信息

一.寻找需要用到的API 在我努力查找之下,我找到了和风天气这个强大又方便的API.接着在平台上注册登录,得到你所需要的key,留以接下来使用 二.用requests获取所需要的数据 根据免费版的url获取 url = "https://free-api.heweather.net/s6/weather/%s?location=%s&key=%s" % (weather_type, parameters, key) page = requests.get(url) page =

python 获取天气信息

[说明]接口为聚合数据接口.API使用说明: 实现代码: import requests,json def main(): #参数 farmat=1 cityname = input("请输入你想查询的城市天气:") key='621043608cb9e7f7f485461ef9e5adef' get_weather(farmat,cityname,key) def get_weather(format,cityname,key): url='http://v.juhe.cn/weat

Python获取当地的天气和任意城市的天气

先从中国天气网得到数据('http://www.weather.com.cn/data/cityinfo/'+城市编码),每个城市都有各自的编码,如何得到用户所在地的城市编码呢?用一个网页就是专门干这个的!http://61.4.185.48:81/g/ 附录:所有城市的编码 101010100=北京 101010200=海淀 101010300=朝阳 101010400=顺义 101010500=怀柔 101010600=通州 101010700=昌平 101010800=延庆 1010109

python 获取一周的天气内容

在学习python,看到一篇获取天气的python脚本,http://xianglong.me/article/get-city-weather-using-python-script/,这个太麻烦了,用beautifulsoup写了一个简单的脚本, # -*- coding: cp936 -*- import sys import httplib import re import requests import json import urllib from bs4 import Beauti

python获取网站http://www.weather.com.cn 城市 8-15天天气

参考一个前辈的代码,修改了一个案例开始学习beautifulsoup做爬虫获取天气信息,前辈获取的是7日内天气, 我看旁边还有8-15日就模仿修改了下.其实其他都没有变化,只变换了获取标签的部分.但是我碰到 一个span获取的问题,如我的案例中每日的源代码是这样的. <li class="t"> <span class="time">周五(19日)</span> <big class="png30 d301&quo

使用Python获取各个城市当前的天气情况

这次的这个项目,弄了好几天,主要在tkinter上做GUI界面上一直卡住,在网上资料又不多,最后直接放弃稍微复杂的东西,直接做个简单点的界面. 程序功能: 1.可以查询不同城市的天气情况和显示时间,每60秒刷新次天气情况,如图:2.可以自由选择城市,选择之后立刻获取该城市的天气情况 关键代码 # _*_ coding: utf-8 _*_ import requests import time def weather_log(stu): #获取实时天气情况写入到文本 cu_time=time.s

Android——使用Volley+fastJson在新线程中读取网络接口获取天气信息

一,关于Volley 其实最初对于网络数据的访问,我是没想到要用框架的,使用HttpURLConnection或者HttpGet or HttpPost都可以实现.但是why? why I have to use Volley?   Before Volley: class HttpURLConnection_post extends Thread{ @Override public void run() { //设置请求的路径 String strUrl="http://api.qingyun

Python 获取网易云音乐热门评论

--> html { line-height: 1.6 } body { font-family: -apple-system-font, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif; background-color: #f3f3f3; line-height: inherit } body.ric