zoukankan      html  css  js  c++  java
  • 给女朋友每日天气预报

    urllib库调用免费的天气API接口,获取所在城市的天气信息(可以通过抓包工具抓取对应城市的citycode)。

    大家应该知道Bing的背景图比较漂亮,每日会更新,获取图片的API也是公开的,可以调用。图片下载到本地 将天气信息和图片利用python的smtp模块邮件发送给女朋友。

    还可以将python程序上传到服务器上,加入一条定时任务,每天定时执行程序。

    # coding=utf-8
    import urllib3
    from email import encoders
    from email.header import Header
    from email.mime.base import MIMEBase
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.utils import parseaddr, formataddr
    import smtplib
    import xml.etree.ElementTree as ET
    import time
    import json,ast
    
    def _format_addr(s):
        name, addr = parseaddr(s)
        print (name,addr)
        headers = Header(name, 'utf-8').encode()
        result =formataddr((headers, addr))
        return result
    
    # 获取bing的每日图片,修改api中的参数n,可以获取多张图片
    # 从bing的api返回的xml中提取图片的url,然后下载到本地,并命名为yyyymmdd.jpg
    def get_bing_pic():
        http = urllib3.PoolManager()
        r = http.request("GET","http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1")
        webpage = r.data
        root = ET.fromstring(webpage)
        # 获取图片链接
        url = 'http://cn.bing.com' + root.find('image').find('url').text
        # 图片描述
        pic_desc_ = root.find('image').find('copyright').text
        # 图片保存到本地的位置
        pic_file_name_ = "d://" + time.strftime('%Y%m%d', time.localtime(time.time()))+".jpg"
        urllib.request.urlretrieve(url, pic_file_name_ )
        #urllib2.urlretrieve(url, pic_file_name_ + url[-4:])
        return pic_file_name_ , pic_desc_
    
    # 调用免费的天气API接口,city_code默认为广州的code:101280101 中山的code:101281701,其他城市code请使用抓包工具
    def get_city_weather(city_code='101281701'):
        http = urllib3.PoolManager()
        r = http.request("GET","http://www.weather.com.cn/data/cityinfo/"+city_code+".html")
        print ((r.data.decode()))
        weather_json = ast.literal_eval(r.data.decode())
        #weather_json= json.load(r.data.decode())
        #response = urllib2.urlopen("http://www.weather.com.cn/data/cityinfo/"+city_code+".html")
        #weather_json = json.loads(r.data.read())
        city_ = weather_json['weatherinfo']['city']
        min_temp_ = weather_json['weatherinfo']['temp1']
        max_temp_ = weather_json['weatherinfo']['temp2']
        weather_ = weather_json['weatherinfo']['weather']
        return city_, min_temp_, max_temp_, weather_
    
    # 发件人地址和密码
    from_addr = 'mylove<xxx@163.com>'
    from_addr1 = 'xxx@163.com'
    password = 'xxxKRCHDKGHIFONZHGO'
    # SMTP服务器地址:
    smtp_server = 'smtp.163.com'
    # 收件人地址:
    to_addr = 'mylove1<xxx@163.com>'
    # 抄送给自己:
    cc_addr = 'mylove<zxxx@163.com>'
    city, min_temp, max_temp, weather = get_city_weather()
    # 带路径的文件名
    pic_file_name, pic_desc = get_bing_pic()
    # 文件名
    pic_name = pic_file_name.split("/")[-1]
    # 这里是邮件正文内容,''是为了将附件的图片插入邮件正文中,直接显示
    content = u"Hello %s今天天气%s,最低温度%s,最高温度%s .<br> %s <br> Have a nice day." % (city, weather, min_temp, max_temp, pic_desc)
    print (content)
    msg = MIMEMultipart()
    msg['From'] = _format_addr(from_addr)
    msg['To'] = _format_addr(to_addr)
    msg['Cc'] = _format_addr(cc_addr)
    #msg['From'] = from_addr
    #msg['To'] = to_addr
    #msg['Cc'] = cc_addr
    msg['Subject'] = Header(u'每日天气播报', 'utf-8').encode()
    msg.attach(MIMEText(content, 'html', 'utf-8'))
    # 添加附件就是加上一个MIMEBase,从本地读取一个图片
    with open(pic_file_name, 'rb') as f:
        # 设置附件的MIME和文件名,这里是jpg类型
        mime = MIMEBase('image', 'jpg', filename=pic_name)
        # 加上必要的头信息
        mime.add_header('Content-Disposition', 'attachment', filename=pic_name)
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        # 把附件的内容读进来
        mime.set_payload(f.read())
        # 用Base64编码
        encoders.encode_base64(mime)
        # 添加到MIMEMultipart
        msg.attach(mime)
    server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
    # server.starttls() #SSL加密的SMTP,注意修改端口号
    server.set_debuglevel(0) # 打印和邮件服务器交互的详细信息
    server.login(from_addr1, password)
    server.sendmail(from_addr, [to_addr, cc_addr], msg.as_string())
    server.quit()
  • 相关阅读:
    ubuntu下如何批量修改文件后缀名
    vanilla
    Ubuntu apt-get 彻底卸载软件包
    Kendall Rank(肯德尔等级)相关系数
    图像质量评估(IQA)
    conda常用命令
    在ubuntu中搜索文件或文件夹的方法
    libstdc++.so.6: version `GLIBCXX_3.4.21' not found
    迅雷磁力链接转BT种子工具
    springboot 集成mybatis plus3
  • 原文地址:https://www.cnblogs.com/Sandy-1128/p/sandy1128-friends.html
Copyright © 2011-2022 走看看