天气预报,邮箱推送,提前预警,告别炎炎夏日下的束手无策

1.分析url

url="http://www.weather.com.cn/weather/101110101.shtml"

2.使用bs4查找所系信息,编写getdata 函数

def getdata(url):
    print("loading")
    datalist=[]
    html=askurl(url)
    soup=BeautifulSoup(html,"html.parser")
    items=soup.find_all('p',class_='wea')
    tems=soup.find_all('p',class_="tem")
    daytemtem=[]
    for tem in tems:
        i=0
        daytemtem.append(tem.text)

    for item in items:
        i=0
        datalist.append(item.text)

    print(datalist)
    print(daytemtem)
    list=[]
    list.append(datalist[0])
    list.append(daytemtem[0])
    nightlist=[]
    nightlist.append(datalist[1])
    nightlist.append(daytemtem[1])


    return list,nightlist

3.消息推送

# -*- coding =utf-8 -*-
# @time : 2022.5.2 10:07
# @software: PyCharm
# Author:Xiao_yu
import re
import datetime
from bs4 import BeautifulSoup
import urllib.request,urllib.error
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
my_sender=input("输入你的QQ邮箱账号:")
my_pass=input("输入你自己QQ邮箱开启smtp后生成的一串代码:")
my_user=input("接受者的邮箱账号:")
def main():

    url="http://www.weather.com.cn/weather/101110101.shtml"
    print("Begin")

    timehour=input("输入时间代号(0为今日天气,1为明天天气):")

    list,nightlist=getdata(url)
    if int(timehour)==0:
        mail(list)
    elif int(timehour)==1:
        mailnight(nightlist)
    else:
        mailfalse()
    print("Over")
def getdata(url):
    print("loading")
    datalist=[]
    html=askurl(url)
    soup=BeautifulSoup(html,"html.parser")
    items=soup.find_all('p',class_='wea')
    tems=soup.find_all('p',class_="tem")
    daytemtem=[]
    for tem in tems:
        i=0
        daytemtem.append(tem.text)

    for item in items:
        i=0
        datalist.append(item.text)

    print(datalist)
    print(daytemtem)
    list=[]
    list.append(datalist[0])
    list.append(daytemtem[0])
    nightlist=[]
    nightlist.append(datalist[1])
    nightlist.append(daytemtem[1])


    return list,nightlist
def askurl(url):
    html=''
    head = { 
        "User-Agent": "Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 80.0.3987.122  Safari / 537.36"
    }
    request=urllib.request.Request(url,headers=head)
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e,"code")
        if hasattr(e,"reason"):
            print(e,"reason")
    return html
def mail(list):
    ret = True
    try:
        msg = MIMEText("The weather in Xi 'an today is "+list[0]+"。"+"/n"+"The temperature is"+list[1],'plain','utf-8')
        msg['From']=formataddr(["The weather forecast",my_sender])
        # msg["To"] = Header(",".join(my_user))
        msg['Subject']='hola my friend, this is your private The weather forecast.'
        server=smtplib.SMTP_SSL("smtp.qq.com",465)
        server.login(my_sender,my_pass)
        server.sendmail(my_sender,my_user,msg.as_string())
        server.quit()
    except Exception:
        ret=False
    return ret
def mailnight(list):
    ret = True
    try:
        msg = MIMEText("Tomorrow "+list[0]+"。"+"\n"+"The temperature is"+list[1],'plain','utf-8')
        msg['From']=formataddr(["The weather forecast",my_sender])
        # msg["To"] = Header(",".join(my_user))
        msg['Subject']='hola my friend, this is your private The weather forecast.'
        server=smtplib.SMTP_SSL("smtp.qq.com",465)
        server.login(my_sender,my_pass)
        server.sendmail(my_sender,my_user,msg.as_string())
        server.quit()
    except Exception:
        ret=False
    return ret
def mailfalse():
    ret = True
    try:
        msg = MIMEText("不在有效时间内",'plain','utf-8')
        msg['From']=formataddr(["Time Wrong",my_sender])
        # msg["To"] = Header(",".join(my_user))
        msg['Subject']='Wrong'
        server=smtplib.SMTP_SSL("smtp.qq.com",465)
        server.login(my_sender,my_pass)
        server.sendmail(my_sender,my_user,msg.as_string())
        server.quit()
    except Exception:
        ret=False
    return ret



if __name__ == "__main__":
    main()
    print("Over")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值