基于百度aip实现语音合成
首先爬取这个网站段子网站来爬取一些文本段子
然后按照baidu语音合成的提示进行操作
from aip import AipSpeech
import requests
from lxml import etree
#获取段子
url = 'http://www.kw-design.net/djpx/116606.htm'
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51'
}
page_text = requests.get(url = url, headers = headers).text
tree = etree.HTML(page_text)
span_list = tree.xpath('//div[@id = "js_content"]/blockquote/p/span/span')
text = ''
for span in span_list:
text += span.xpath('./text()')[0]
#生成mp3
#下面这三个输入自己的语音合成应用的相关信息
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(text, 'zh', 1, {
'vol' : 5,
})
if not isinstance(result, dict):
with open('./audio.mp3', 'wb') as f:
f.write(result)