Python定时发送天气预报
侧边栏壁纸
  • 累计撰写 23 篇文章
  • 累计收到 12 条评论

Python定时发送天气预报

怪怪的科长
2021-04-21 / 1 评论 / 54 阅读 / 正在检测是否收录...
import requests
import smtplib
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from pyquery import PyQuery as pq

def sendWeatherMail(url,to):
    #获取天气信息
    html = requests.get(url).content
    doc = pq(html)
    clearfix1 = doc('#today .clearfix')
    clearfix2 = clearfix1.remove('.sky').text()
    a = clearfix2.split()

    wea = str(a[0]+':\n'+a[1]+' '+a[2]+' 风速:'+a[3]+' '+a[4]+' '+a[5]+'\n\n'+a[6]+':\n'+a[7]+' '+a[8]+' 风速:'+a[9]+' '+a[10]+' '+a[11])


    #发送邮件
    msg = MIMEText(wea)
    msg['Subject'] = 'XX地区天气预报'
    msg['From'] = 'admin@XXXXX.com'    #邮箱账号
    msg['To'] = to

    mail_host = 'smtpdm.aliyun.com'    #我这里用的阿里云推送 163 QQ 均可
    mail_user = 'admin@XXXXX.com'    #自己的邮箱账号
    mail_pass = 'XXXXXXXXXXXX'        #你的那一串key

    s = smtplib.SMTP_SSL(mail_host)
    s.connect(mail_host, 465)   
    s.login(mail_user,mail_pass)
    s.send_message(msg)
    s.quit()


    

try:
    sendWeatherMail('http://www.weather.com.cn/weather1d/101010300.shtml#input','86593927@qq.com')#101010300为北京市朝阳区城市编号 可更改地区编号 86593927@qq.com改为发送对象的邮箱地址
    print('邮件发送成功')
except smtplib.SMTPException:
    print('邮件发送失败')
0

评论

博主关闭了所有页面的评论