python如何實現(xiàn)智能語音天氣預報-創(chuàng)新互聯(lián)

小編給大家分享一下python如何實現(xiàn)智能語音天氣預報,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:網(wǎng)站設計、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的宜昌網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

python編寫的語音天氣預報

本系統(tǒng)主要包括四個函數(shù):

1、獲取天氣數(shù)據(jù)

1、輸入要查詢天氣的城市

2、利用urllib模塊向中華萬年歷天氣api接口請求天氣數(shù)據(jù)

3、利用gzip解壓獲取到的數(shù)據(jù),并編碼utf-8

4、利用json轉(zhuǎn)化成python識別的數(shù)據(jù),返回為天氣預報數(shù)據(jù)復雜形式的字典(字典中的字典)

2、輸出當天天氣數(shù)據(jù)

1、格式化輸出當天天氣,包括:天氣狀況,此時溫度,最高溫度、最低溫度,風級,風向等。

3,語音播報當天天氣

1、創(chuàng)建要輸出的語音文本(weather_forecast_txt)

2、利用百度的語音合成模塊AipSpeech,合成語音文件

3,利用playsound模塊播放語音

4、未來幾天溫度變化趨勢

1、創(chuàng)建未來幾天高低溫數(shù)據(jù)的字典

2,利用matplotlib模塊,圖形化溫度變化趨勢

5、代碼

#導入必要模塊
import urllib.parse
import urllib.request
import gzip
import json
import playsound
from aip import AipSpeech
import matplotlib.pyplot as plt
import re
#設置參數(shù),圖片顯示中文字符,否則亂碼
plt.rcParams['font.sans-serif']=['SimHei']
#定義獲取天氣數(shù)據(jù)函數(shù)
def Get_weather_data():
  print('------天氣查詢------')
  city_name = input('請輸入要查詢的城市名稱:')
  url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
  weather_data = urllib.request.urlopen(url).read()
  # 讀取網(wǎng)頁數(shù)據(jù)
  weather_data = gzip.decompress(weather_data).decode('utf-8')
  # #解壓網(wǎng)頁數(shù)據(jù)
  weather_dict = json.loads(weather_data)
  return weather_dict
#定義當天天氣輸出格式
def Show_weather(weather_data):
  weather_dict = weather_data
  if weather_dict.get('desc') == 'invilad-citykey':
    print('你輸入的城市有誤或未收錄天氣,請重新輸入...')
  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('風級:', forecast[0].get('fengli').split('<')[2].split(']')[0])
    print('風向:', forecast[0].get('fengxiang'))
    weather_forecast_txt = '您好,您所在的城市%s,' \
                '天氣%s,' \
                '當前溫度%s,' \
                '今天最高溫度%s,' \
                '最低溫度%s,' \
                '風級%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
#定義語音播報今天天氣狀況
def Voice_broadcast(weather_forcast_txt):
  weather_forecast_txt = weather_forcast_txt
  APP_ID = 你的百度語音APP_ID
  API_KEY = 你的百度語音API_KEY
  SECRET_KEY = 你的百度語音SECRET_KEY
  client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  print('語音提醒:', weather_forecast_txt)
  #百度語音合成
  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模塊播放語音
  playsound.playsound(r'C:\Users\ban\Desktop\bsy\sound2.mp3')
#未來四天天氣變化圖
def Future_weather_states(forecast):
  future_forecast = forecast
  dict={}
  #獲取未來四天天氣狀況
  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('最近幾天溫度變化趨勢')
  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如何實現(xiàn)智能語音天氣預報

以上是“python如何實現(xiàn)智能語音天氣預報”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當前標題:python如何實現(xiàn)智能語音天氣預報-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://muchs.cn/article0/cshgio.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設、微信公眾號網(wǎng)站策劃、網(wǎng)站設計自適應網(wǎng)站、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化