python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)
python編寫(xiě)的語(yǔ)音天氣預(yù)報(bào)
本系統(tǒng)主要包括四個(gè)函數(shù):
1、獲取天氣數(shù)據(jù)
1、輸入要查詢天氣的城市
2、利用urllib模塊向中華萬(wàn)年歷天氣api接口請(qǐng)求天氣數(shù)據(jù)
3、利用gzip解壓獲取到的數(shù)據(jù),并編碼utf-8
4、利用json轉(zhuǎn)化成python識(shí)別的數(shù)據(jù),返回為天氣預(yù)報(bào)數(shù)據(jù)復(fù)雜形式的字典(字典中的字典)
2、輸出當(dāng)天天氣數(shù)據(jù)
1、格式化輸出當(dāng)天天氣,包括:天氣狀況,此時(shí)溫度,最高溫度、最低溫度,風(fēng)級(jí),風(fēng)向等。
3,語(yǔ)音播報(bào)當(dāng)天天氣
1、創(chuàng)建要輸出的語(yǔ)音文本(weather_forecast_txt)
2、利用百度的語(yǔ)音合成模塊AipSpeech,合成語(yǔ)音文件
3,利用playsound模塊播放語(yǔ)音
4、未來(lái)幾天溫度變化趨勢(shì)
1、創(chuàng)建未來(lái)幾天高低溫?cái)?shù)據(jù)的字典
2,利用matplotlib模塊,圖形化溫度變化趨勢(shì)
5、代碼
#導(dǎo)入必要模塊 import urllib.parse import urllib.request import gzip import json import playsound from aip import AipSpeech import matplotlib.pyplot as plt import re #設(shè)置參數(shù),圖片顯示中文字符,否則亂碼 plt.rcParams['font.sans-serif']=['SimHei'] #定義獲取天氣數(shù)據(jù)函數(shù) def Get_weather_data(): print('------天氣查詢------') city_name = input('請(qǐng)輸入要查詢的城市名稱:') url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name) weather_data = urllib.request.urlopen(url).read() # 讀取網(wǎng)頁(yè)數(shù)據(jù) weather_data = gzip.decompress(weather_data).decode('utf-8') # #解壓網(wǎng)頁(yè)數(shù)據(jù) weather_dict = json.loads(weather_data) return weather_dict #定義當(dāng)天天氣輸出格式 def Show_weather(weather_data): weather_dict = weather_data if weather_dict.get('desc') == 'invilad-citykey': print('你輸入的城市有誤或未收錄天氣,請(qǐng)重新輸入...') elif weather_dict.get('desc') == 'OK': forecast = weather_dict.get('data').get('forecast') print('日期:', forecast[0].get('date')) print('城市:', weather_dict.get('data').get('city')) print('天氣:', forecast[0].get('type')) print('溫度:', weather_dict.get('data').get('wendu') + '℃ ') print('高溫:', forecast[0].get('high')) print('低溫:', forecast[0].get('low')) print('風(fēng)級(jí):', forecast[0].get('fengli').split('<')[2].split(']')[0]) print('風(fēng)向:', forecast[0].get('fengxiang')) weather_forecast_txt = '您好,您所在的城市%s,' \ '天氣%s,' \ '當(dāng)前溫度%s,' \ '今天最高溫度%s,' \ '最低溫度%s,' \ '風(fēng)級(jí)%s,' \ '溫馨提示:%s' % \ ( weather_dict.get('data').get('city'), forecast[0].get('type'), weather_dict.get('data').get('wendu'), forecast[0].get('high'), forecast[0].get('low'), forecast[0].get('fengli').split('<')[2].split(']')[0], weather_dict.get('data').get('ganmao') ) return weather_forecast_txt,forecast #定義語(yǔ)音播報(bào)今天天氣狀況 def Voice_broadcast(weather_forcast_txt): weather_forecast_txt = weather_forcast_txt APP_ID = 你的百度語(yǔ)音APP_ID API_KEY = 你的百度語(yǔ)音API_KEY SECRET_KEY = 你的百度語(yǔ)音SECRET_KEY client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) print('語(yǔ)音提醒:', weather_forecast_txt) #百度語(yǔ)音合成 result = client.synthesis(weather_forecast_txt, 'zh', 1, {'vol': 5}) if not isinstance(result, dict): with open('sound2.mp3', 'wb') as f: f.write(result) f.close() #playsound模塊播放語(yǔ)音 playsound.playsound(r'C:\Users\ban\Desktop\bsy\sound2.mp3') #未來(lái)四天天氣變化圖 def Future_weather_states(forecast): future_forecast = forecast dict={} #獲取未來(lái)四天天氣狀況 for i in range(5): data = [] date=future_forecast[i]['date'] date = int(re.findall('\d+',date)[0]) data.append(int(re.findall('\d+',future_forecast[i]['high'])[0])) data.append(int(re.findall('\d+', future_forecast[i]['low'])[0])) data.append(future_forecast[i]['type']) dict[date] = data data_list = sorted(dict.items()) date=[] high_temperature = [] low_temperature = [] for each in data_list: date.append(each[0]) high_temperature.append(each[1][0]) low_temperature.append(each[1][1]) fig = plt.plot(date,high_temperature,'r',date,low_temperature,'b') plt.xlabel('日期') plt.ylabel('℃') plt.legend(['高溫','低溫']) plt.xticks(date) plt.title('最近幾天溫度變化趨勢(shì)') plt.show() #主函數(shù) if __name__=='__main__': weather_data = Get_weather_data() weather_forecast_txt, forecast = Show_weather(weather_data) Future_weather_states(forecast) Voice_broadcast(weather_forecast_txt)
6、最終效果
以上這篇python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python pandas.DataFrame調(diào)整列順序及修改index名的方法
這篇文章主要介紹了Python pandas.DataFrame調(diào)整列順序及修改index名的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python遠(yuǎn)程控制Windows服務(wù)器的方法總結(jié)
在信息時(shí)代的洪流中,掌握一門編程語(yǔ)言已經(jīng)成為一項(xiàng)必備技能,Python,這門以簡(jiǎn)潔、易學(xué)、強(qiáng)大著稱的編程語(yǔ)言,更是成為無(wú)數(shù)開(kāi)發(fā)者的大寶劍,今天,我們要探討的就是如何用 Python 遠(yuǎn)程控制 Windows 服務(wù)器,需要的朋友可以參考下2024-07-07torch.utils.data.DataLoader與迭代器轉(zhuǎn)換操作
這篇文章主要介紹了torch.utils.data.DataLoader與迭代器轉(zhuǎn)換操作,文章內(nèi)容接受非常詳細(xì),對(duì)正在學(xué)習(xí)或工作的你有一定的幫助,需要的朋友可以參考一下2022-02-02關(guān)于Python Socket編程的要點(diǎn)詳解
Socket是網(wǎng)絡(luò)編程的一個(gè)抽象概念,通常我們用一個(gè)Socket表示“打開(kāi)了一個(gè)網(wǎng)絡(luò)鏈接”,而打開(kāi)一個(gè)Socket需要知道目標(biāo)計(jì)算機(jī)的IP地址和端口號(hào),再指定協(xié)議類型即可,這篇文章主要給大家介紹了關(guān)于Python Socket編程的相關(guān)資料,需要的朋友可以參考下2021-08-08Python?使用BeautifulSoup庫(kù)的方法
BeautifulSoup庫(kù)用于從HTML或XML文件中提取數(shù)據(jù),它可以自動(dòng)將復(fù)雜的HTML文檔轉(zhuǎn)換為樹(shù)形結(jié)構(gòu),并提供簡(jiǎn)單的方法來(lái)搜索文檔中的節(jié)點(diǎn),使得我們可以輕松地遍歷和修改HTML文檔的內(nèi)容,本文給大家介紹Python?使用BeautifulSoup庫(kù)的方法,感興趣的朋友一起看看吧2023-10-10零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用urllib2組件抓取網(wǎng)頁(yè)內(nèi)容
文章詳細(xì)介紹了在python2.5環(huán)境下,如何使用urllib2這個(gè)python自帶的組件進(jìn)行抓取指定網(wǎng)頁(yè)內(nèi)容的,整個(gè)過(guò)程記錄的非常的詳細(xì),也很簡(jiǎn)單,有需要的朋友可以參考下,寫(xiě)出自己的python爬蟲(chóng)2014-11-11Pytorch 實(shí)現(xiàn)權(quán)重初始化
今天小編就為大家分享一篇Pytorch 實(shí)現(xiàn)權(quán)重初始化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12python進(jìn)程類subprocess的一些操作方法例子
這篇文章主要介紹了python進(jìn)程類subprocess的一些操作方法例子,本文講解了Popen、wait、poll、kill、communicate等方法的實(shí)際操作例子,需要的朋友可以參考下2014-11-11