1. 程式人生 > >python獲取天氣狀況並以郵件的形式發到目的郵箱

python獲取天氣狀況並以郵件的形式發到目的郵箱

ucc cto size 頻繁 red http borde ble 星期四

python爬取天氣情況

技術分享圖片

下面為示例代碼:

from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.error import HTTPError
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import time

"""
爬蟲程序是一個需要後期投入很大維護力度的,就比如網頁開發者將來在某一時間重構了html代碼,這就很有可能導致爬蟲程序的失敗,所以寫爬蟲程序要盡量做到未雨綢繆,讓爬蟲程序更健壯,這樣你才能睡一個安穩的覺
這個程序很簡單,大體分為兩個部分,爬蟲部分和郵件部分,代碼還有很大的優化空間,比如一些常量可以拿到外面,這樣代碼看起來會更整潔,郵件內容是以html格式的形式發送的,這樣你就可以改成自己喜歡的樣式。
我不建議你頻繁的去發郵件,郵箱可能會被封掉,信件退回,導致發送失敗
發送電子郵件的郵箱要開啟smtp客戶端功能,郵箱——>設置——>開啟SMTP服務,獲取授權碼(就是程序裏需要登陸郵箱的密碼)
"""
#獲取當天的天氣情況 def get_weather(url): try: html=urlopen(url).read()
except HTTPError as e: return None try: weather_list=[] bs0bj=BeautifulSoup(html,"html.parser") time.sleep(5) weather=bs0bj.find("div",{"class":"condition-icon wx-weather-icon vector"}).next_siblings title=bs0bj.body.h1.get_text() weather_list.append(title)
for next in weather: weather_list.append(next.get_text()) except AttributeError as e: return None return weather_list
#獲取未來5天的天氣情況
def get_5weathers(url): try: html=urlopen(url).read() except HTTPError as e: return None try: weather5_list=[] bs0bj=BeautifulSoup(html,"html.parser") weathers=bs0bj.find("table",{"class":"twc-table"}).tbody for child in weathers.children: list1=[] for i in child.children: list1.append(i.get_text()) list1.remove("") weather5_list.append(list1) except AttributeError as e: return None return weather5_list
#等到的數據形如一下數據格式
# weather=[‘北京, 中國‘, ‘3°‘, ‘晴朗‘, ‘體感溫度 -1°‘, ‘高溫 -- 低溫 -7°紫外線指數 0(最大值10)‘] # weathers=[[‘今天晚上\n12月 18日‘, ‘大部晴朗‘, ‘---7°‘, ‘0%‘, ‘北 15 公裏 /小時 ‘, ‘40%‘], [‘星期二12月 19日‘, ‘晴朗‘, ‘5°-5°‘, ‘0%‘, ‘西南 21 公裏 /小時 ‘, ‘32%‘], [‘星期三12月 20日‘, ‘晴朗‘, ‘7°-6°‘, ‘0%‘, ‘西北 22 公裏 /小時 ‘, ‘33%‘], [‘星期四12月 21日‘, ‘晴朗‘, ‘6°-6°‘, ‘0%‘, ‘西南西 11 公裏 /小時 ‘, ‘41%‘], [‘星期五12月 22日‘, ‘晴朗‘, ‘8°-6°‘, ‘0%‘, ‘北 16 公裏 /小時 ‘, ‘30%‘], [‘星期六12月 23日‘, ‘晴朗‘, ‘8°-3°‘, ‘0%‘, ‘西北西 14 公裏 /小時 ‘, ‘29%‘]]
# #***********************發送電子郵件******************************* #第三方SMTP服務器 def sendEmail(): msg=MIMEText(mail_msg,"html","utf-8") msg["From"]=formataddr(["褲襠人",mail_user]) msg["To"]=formataddr(["小褲襠人s",receive_address]) msg["Subject"]="北京天氣預報" try: smtp0bj=smtplib.SMTP_SSL(mail_host,465) smtp0bj.login(mail_user,mail_pass) smtp0bj.sendmail(mail_user,receive_address,msg.as_string()) smtp0bj.quit() print("mail has been send to %s successfully."%receive_address) except smtplib.SMTPException as e: print(e) if __name__=="__main__": weather=get_weather("http://www.weather.com") weathers=get_5weathers("https://weather.com/zh-CN/weather/5day/l/CHXX0008:1:CH") if weather==None: exit() if weathers==None: exit() mail_host="smtp.163.com" mail_user="150XXxX[email protected]" #發件人郵箱賬號 mail_pass="jXXxX199XXxX" #發件人郵箱密碼 receives=[ "4352XXxX@qq.com", "3426XXxX@qq.com", "5437XXxX[email protected]", "6353XXxX[email protected]", ] #收件人郵箱賬號 mail_msg=""" <h1>褲襠人天氣預報</h1> <p style="color:#99cc00;font-size:15px">%s-->目前天氣狀況:溫度%s ,%s ,%s ,%s </p> <h3>以下是未來5天的天氣情況</h3> <table width="800px" border="0" cellspacing="1" cellpadding="0" style="text-align:center"> <thead style="background-color:#3399ff"> <tr style="line-height:40px;"> <th>白天</th> <th>說明</th> <th>高/低</th> <th>降雨概率</th> <th>風力</th> <th>濕度</th> </tr> </thead> <tbody> <tr style="background: #ccffcc"> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> <tr style="background: #ccffcc"> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> <tr style="background: #ccffcc"> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> <tr style="background: #ccffcc"> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> <tr style="background: #ccffcc"> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> </tbody> </table> <p style="color:red;font-size:10px">註意:每天早上八點準時發送郵件,希望小夥伴們,多多關註天氣情況,註意保暖!</p> """%(weather[0],weather[1],weather[2],weather[3],weather[4],weathers[0][0],weathers[0][1],weathers[0][2],weathers[0][3],weathers[0][4],weathers[0][5],weathers[1][0],weathers[1][1],weathers[1][2],weathers[1][3],weathers[1][4],weathers[1][5],weathers[2][0],weathers[2][1],weathers[2][2],weathers[2][3],weathers[2][4],weathers[2][5],weathers[3][0],weathers[3][1],weathers[3][2],weathers[3][3],weathers[3][4],weathers[3][5],weathers[4][0],weathers[4][1],weathers[4][2],weathers[4][3],weathers[4][4],weathers[4][5]) for receive_address in receives: sendEmail() time.sleep(120) exit()

python獲取天氣狀況並以郵件的形式發到目的郵箱