1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
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['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['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['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")
|