python语⾳播报天⽓预报_python实现智能语⾳天⽓预报python编写的语⾳天⽓预报
本系统主要包括四个函数:
1、获取天⽓数据
1、输⼊要查询天⽓的城市
2、利⽤urllib模块向中华万年历天⽓api接⼝请求天⽓数据
3、利⽤gzip解压获取到的数据,并编码utf-8
4、利⽤json转化成python识别的数据,返回为天⽓预报数据复杂形式的字典(字典中的字典)
2、输出当天天⽓数据
1、格式化输出当天天⽓,包括:天⽓状况,此时温度,最⾼温度、最低温度,风级,风向等。
3,语⾳播报当天天⽓
1、创建要输出的语⾳⽂本(weather_forecast_txt)
2、利⽤百度的语⾳合成模块AipSpeech,合成语⾳⽂件
3,利⽤playsound模块播放语⾳
4、未来⼏天温度变化趋势
1、创建未来⼏天⾼低温数据的字典
2,利⽤matplotlib模块,图形化温度变化趋势
5、代码
#导⼊必要模块
import urllib.par
quest
dwordimport gzip
import json
import playsound
from aip import AipSpeech
import matplotlib.pyplot as plt
import re
#设置参数,图⽚显⽰中⽂字符,否则乱码
#定义获取天⽓数据函数
def Get_weather_data():
print('------天⽓查询------')
city_name = input('请输⼊要查询的城市名称:')
url = 'uch/weather_mini?city=' + urllib.par.quote(city_name)
weather_data = quest.urlopen(url).read()
# 读取⽹页数据
weather_data = gzip.decompress(weather_data).decode('utf-8') # #解压⽹页数据
weather_dict = json.loads(weather_data)
return weather_dict
#定义当天天⽓输出格式
def Show_weather(weather_data):
weather_dict = weather_data
if ('desc') == 'invilad-citykey':
print('你输⼊的城市有误或未收录天⽓,请重新输⼊...')
elif ('desc') == 'OK':
forecast = ('data').get('forecast')
print('⽇期:', forecast[0].get('date'))
the mechanic
print('城市:', ('data').get('city'))
print('天⽓:', forecast[0].get('type'))
print('温度:', ('data').get('wendu') + '℃ ')
print('⾼温:', forecast[0].get('high'))
print('低温:', forecast[0].get('low'))
print('风级:', forecast[0].get('fengli').split('
snowwhiteprint('风向:', forecast[0].get('fengxiang'))
weather_forecast_txt = '您好,您所在的城市%s,' \
'天⽓%s,' \
'当前温度%s,' \
'今天最⾼温度%s,' \
'最低温度%s,' \
'风级%s,' \
benefit的用法'温馨提⽰:%s' % \
(
('data').get('city'),
forecast[0].get('type'),
男士青春痘的治疗方法
('data').get('wendu'),
forecast[0].get('high'),
forecast[0].get('low'),
forecast[0].get('fengli').split('
('data').get('ganmao')
affordability
)
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.clo()
#playsound模块播放语⾳
playsound.playsound(r'C:\Urs\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.title('最近⼏天温度变化趋势')
plt.show()
#主函数
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实现智能语⾳天⽓预报就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持脚本之家。