zoukankan      html  css  js  c++  java
  • 读取天气信息,并通过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)
  • 相关阅读:
    combo参数配置_手册
    mysql服务器辅助选项
    CentOS中操作
    Linux PHP增加JSON支持及如何使用JSON
    linux服务器命令
    linux中的工具
    linux文件夹操作(及模糊搜索)
    治疗肾结石
    其他书籍
    如何定位到div滚动条的最底端
  • 原文地址:https://www.cnblogs.com/liuyang92/p/7360743.html
Copyright © 2011-2022 走看看