• 读取天气信息,并通过QQ邮箱发送至指定邮箱


    from email.mime.text import MIMEText
    from email.header import Header
    from smtplib import SMTP_SSL
    
    import urllib.request
    import time
    
    def get_weather_msg():
        page = urllib.request.urlopen("http://www.weather.com.cn/weather/101200101.shtml")
        text = page.read().decode("utf8")
        #print(text)
        startNum = text.find("(今天)</h1>")
        endNum = text.find("(明天)</h1>")
        text = text[startNum-3:endNum]
        #print(text)
        date = text[0:3]
        #print("**************")
        startNum = text.find("class="wea">")
        endNum = text.find("<p class="tem">")
        weather = text[startNum+12:endNum-5]
    
        startNum = text.find("<p class="tem">")
        endNum = text.find("℃</i>")
        tempreture = text[endNum-2:endNum]
    
        message = ("今天" + date + "
    " + "天气:"+ weather+ "
    " + "温度:"+ tempreture +"" + "
    ")
        tempreture = float(tempreture)
        if tempreture > 30:
            message = message + "高温"
        elif tempreture < 10:
            message = message + "低温"
        else:
            message = message + "温度适中"
            
        #print("今天" + date)
        #print("天气:"+ weather)
        #print("温度:"+ tempreture +"℃")
        return (message)
    
    def send_weather_by_mail(content):
        #qq邮箱smtp服务器
        host_server = 'smtp.qq.com'
        #sender_qq为发件人的qq号码
        sender_qq = '150xxxxx'
        #pwd为qq邮箱的授权码
        pwd = 'Lxxxxx'
        #发件人的邮箱
        sender_qq_mail = '150xxxxx@qq.com'
        #收件人邮箱
        receiver = 'xxxxx@xxxx.com'
        #邮件的正文内容
        mail_content = content
        #邮件标题
        mail_title = 'xxx的邮件'
    
        #ssl登录
        smtp = SMTP_SSL(host_server)
        #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
        smtp.set_debuglevel(0)
        smtp.ehlo(host_server)
        smtp.login(sender_qq, pwd)
    
        msg = MIMEText(mail_content, "plain", 'utf-8')
        msg["Subject"] = Header(mail_title, 'utf-8')
        msg["From"] = sender_qq_mail
        msg["To"] = receiver
        smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
        smtp.quit()     
    
    
    checkTime = 0
    while 1:
        checkTime += 1
        send_weather_by_mail(get_weather_msg())
        print(">>>"+str(checkTime)+">>>")
        time.sleep(10)
  • 相关阅读:
    用document.onreadystatechange和document.readyState确保文档加载完毕才获取DOM
    动态修改样式和层叠样式表
    jQuery中Ajax事件顺序及各参数含义
    对于JavaScript对象的prototype和__proto__的理解
    HTML5实现“摇一摇”效果
    修改mysql错误提示语言的方法
    12个非常有用的JavaScript小技巧
    学习javascript中this用法的一些感悟
    Token 认证
    “好”的接口是怎么样的?
  • 原文地址:https://www.cnblogs.com/liuyang92/p/7360743.html
Copyright © 2020-2023  润新知