python语音播报天气预报_python实现智能语音天气预报

python语音播报天气预报_python实现智能语音天气预报

2023年7月10日发(作者:)

python语⾳播报天⽓预报_python实现智能语⾳天⽓预报python编写的语⾳天⽓预报本系统主要包括四个函数:1、获取天⽓数据1、输⼊要查询天⽓的城市2、利⽤urllib模块向中华万年历天⽓api接⼝请求天⽓数据3、利⽤gzip解压获取到的数据,并编码utf-84、利⽤json转化成python识别的数据,返回为天⽓预报数据复杂形式的字典(字典中的字典)2、输出当天天⽓数据1、格式化输出当天天⽓,包括:天⽓状况,此时温度,最⾼温度、最低温度,风级,风向等。3,语⾳播报当天天⽓1、创建要输出的语⾳⽂本(weather_forecast_txt)2、利⽤百度的语⾳合成模块AipSpeech,合成语⾳⽂件3,利⽤playsound模块播放语⾳4、未来⼏天温度变化趋势1、创建未来⼏天⾼低温数据的字典2,利⽤matplotlib模块,图形化温度变化趋势5、代码#导⼊必要模块import mport timport gzipimport jsonimport playsoundfrom aip import AipSpeechimport as pltimport re#设置参数,图⽚显⽰中⽂字符,否则乱码ms['-serif']=['SimHei']#定义获取天⽓数据函数def Get_weather_data():print('------天⽓查询------')city_name = input('请输⼊要查询的城市名称:')url = '/weather_mini?city=' + (city_name)weather_data = n(url).read()# 读取⽹页数据weather_data = ress(weather_data).decode('utf-8')# #解压⽹页数据weather_dict = (weather_data)return weather_dict#定义当天天⽓输出格式def Show_weather(weather_data):weather_dict = weather_dataif weather_('desc') == 'invilad-citykey':print('你输⼊的城市有误或未收录天⽓,请重新输⼊...')elif weather_('desc') == 'OK':forecast = weather_('data').get('forecast')print('⽇期:', forecast[0].get('date'))print('城市:', weather_('data').get('city'))print('天⽓:', forecast[0].get('type'))print('温度:', weather_('data').get('wendu') + '℃ ')print('⾼温:', forecast[0].get('high'))print('低温:', forecast[0].get('low'))print('风级:', forecast[0].get('fengli').split('print('风向:', forecast[0].get('fengxiang'))weather_forecast_txt = '您好,您所在的城市%s,' '天⽓%s,' '当前温度%s,' '今天最⾼温度%s,' '最低温度%s,' '风级%s,' '温馨提⽰:%s' % (weather_('data').get('city'),forecast[0].get('type'),weather_('data').get('wendu'),forecast[0].get('high'),forecast[0].get('low'),forecast[0].get('fengli').split('weather_('data').get('ganmao'))return weather_forecast_txt,forecast#定义语⾳播报今天天⽓状况def Voice_broadcast(weather_forcast_txt):weather_forecast_txt = weather_forcast_txtAPP_ID = 你的百度语⾳APP_IDAPI_KEY = 你的百度语⾳API_KEYSECRET_KEY = 你的百度语⾳SECRET_KEYclient = AipSpeech(APP_ID, API_KEY, SECRET_KEY)print('语⾳提醒:', weather_forecast_txt)#百度语⾳合成result = sis(weather_forecast_txt, 'zh', 1, {'vol': 5})if not isinstance(result, dict):with open('3', 'wb') as f:(result)()#playsound模块播放语⾳und(r'C:3')#未来四天天⽓变化图def Future_weather_states(forecast):future_forecast = forecastdict={}#获取未来四天天⽓状况for i in range(5):data = []date=future_forecast[i]['date']date = int(l('d+',date)[0])(int(l('d+',future_forecast[i]['high'])[0]))(int(l('d+', future_forecast[i]['low'])[0]))(future_forecast[i]['type'])dict[date] = datadata_list = sorted(())date=[]high_temperature = []low_temperature = []for each in data_list:(each[0])high_(each[1][0])low_(each[1][1])fig = (date,high_temperature,'r',date,low_temperature,'b')('⽇期')('℃')(['⾼温','低温'])(date)('最近⼏天温度变化趋势')()#主函数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实现智能语⾳天⽓预报就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持脚本之家。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688932614a184976.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信