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")