https://files.cnblogs.com/files/littlehb/%E6%A0%91%E8%8E%93%E6%B4%BE%E6%92%AD%E6%94%BE%E5%A4%A9%E6%B0%94.zip
from aip import AipSpeech
import urllib.request
import json
import time
import pygame
# pip install pygame
# cd python-sdk-master
# python3 setup.py install
# 注册的baidu帐号
APP_ID = '10892266'
API_KEY = '1qVkEPMsjr7G0lswIqqDegHT'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxx'
# 获取天气
def getWeather():
ApiUrl = "http://www.weather.com.cn/data/sk/101060101.html"
html = urllib.request.urlopen(ApiUrl)
# 读取并解码
data = html.read().decode("utf-8")
# 将JSON编码的字符串转换回Python数据结构
ss = json.loads(data)
info = ss['weatherinfo']
return info
if __name__ == '__main__':
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 获取天气信息
info=getWeather()
speakString='现在播报天气:城市,%s' % info['city']
speakString=speakString+(' ,温度:%s度' % info['temp'])
speakString = speakString +(',风速:%s' % info['WD'])
speakString = speakString + info['WS']
speakString = speakString +(',湿度:%s' % info['SD'])
speakString = speakString + (',天气播报完毕!')
print(speakString)
# 调用百度进行生成语音
result = client.synthesis(speakString, 'zh', 1, {
'vol': 5,
})
# 保存文件
file='weather.mp3'
if not isinstance(result, dict):
with open(file, 'wb') as f:
f.write(result)
# 播放音乐
# 让树莓派播放:mplayer shero.mp3
pygame.mixer.init()
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(1)