python实现智能语音天气预报
作者:飞奔的帅帅 发布时间:2023-05-13 14:10:27
标签: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.parse
import urllib.request
import gzip
import json
import playsound
from aip import AipSpeech
import matplotlib.pyplot as plt
import re
#设置参数,图片显示中文字符,否则乱码
plt.rcParams['font.sans-serif']=['SimHei']
#定义获取天气数据函数
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()
# 读取网页数据
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 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()
#主函数
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、最终效果
来源:https://blog.csdn.net/ustbbsy/article/details/79486961
0
投稿
猜你喜欢
- google前不久推出了Google Ajax Library API,说白了就是提供几个知名js库的host,目前有:jQueryprot
- javascript时间戳和日期字符串相互转换代码(超简单)<html xmlns="http://www.w3.org/1
- 目录查找和修复数据表冲突更新索引统计查找和修复数据表冲突数据表最糟糕的事情就是发生冲突。使用MyISAM存储引擎时,通常因为崩溃导致冲突。然
- 写在前面关于 ES6, 也终于在 2015 年的 7 月 18 号尘埃落定了。虽然说各大浏览器还没有全面的支持,不过这并不妨碍我们一颗想要撸
- 前言:协程(coroutine)是 Go 语言最大的特色之一,goroutine 的实现其实是通过协程。协程的概念协程一词最早出现在 196
- 前言这篇文章主要介绍了pyinstaller打包opencv和numpy程序运行错误解决,文中通过示例代码介绍的非常详细,对大家的学习或者工
- 本文讲述一个用Python写的小程序,用于有注入点的链接,以检测当前数据库用户是否为sa,详细代码如下:# Code by zhaoxiao
- 稀疏矩阵格式 coo_matrixcoo_matrix是最简单的稀疏矩阵存储方式,采用三元组(row, col, data)(或称为ijv
- 本文实例讲述了Python中str.join()简单用法。分享给大家供大家参考,具体如下:Python join() 方法用于将序列中的元素
- python要知道怎么用好编译器。当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件。要运行代码
- 当我们的程序报错时,解释器会将整个异常的堆栈信息全部输出出来,举个例子:def foo(): &
- 一、说明早上看到Python使用pickle进行序列化和反序列化,然后发现面临的一个获取不到返回值的框架,似乎可以通过在框架中先序列化,然后
- 所谓产品其实最终展现在用户面前的只是界面而已,所谓界面绝大多数时候只包括两个部分:图片、文字。重视界面上的每一个像素和每一个文字是UED的基
- MySQL是关系型数据库中的明星,MongoDB是文档型数据库中的翘楚。下面通过一个设计实例对比一下二者:假设我们正在维护一个手机产品库,里
- 在网页上,有一些内容是通过执行Ajax请求动态加载数据渲染出来的。对于需要获取这些内容的需求,我们可以使用Python来实现数据的抓取。Aj
- 本文实例讲述了Python实现从订阅源下载图片的方法。分享给大家供大家参考。具体如下:这段代码是基于python 3.4实现的,和pytho
- 本文实例讲述了php版本CKEditor 4和CKFinder安装及配置方法。分享给大家供大家参考,具体如下:下载并解压CKEditor 4
- #!/usr/bin/env python# coding=utf-8#----------------------------------
- 1、利用uuid函数生成唯一且不规则的主键idsql:CREATE TABLE `test` ( `id` varchar(10
- 在触屏设备上,一些比较基础的手势都需要通过对 touch 事件进行二次封装才能实现。zepto 是移动端上使用率比较高的一个类库,但是其 t