同名csdn文章:https://blog.csdn.net/bibinGee/article/details/104196406,欢迎转载
介绍一个文字转语音的Python库:pyttsx3。该库可以进行离线文字转语音,可以满足一般的对文字进行语音转换的需求。只是音调表比较生硬,声音表情不够丰富(基本没有),目前对于中文只支持女音,英文可以支持男女音,所以对于一些要求不高的场合pyttsx3还是比较合适的。
pyttsx3库安装比较简单:
pip install pyttsx3
安装完成就可以使用了,可以使用以下代码进行测试:
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
接下来我们利用pyttsx3和urllib, beautifulsoup进行天气信息的获取和语音转文字的操作
beautifulsoup需要单独安装
pip install beautilfulsoup4
天气信息可以从中国天气网(http://www.weather.com.cn/)进行提取,如我这里提取的是广东东莞市市区的天气情况,则在搜索栏输入“东莞”跳转只“广东东莞城区”的天气预报网页,这里我们得到“东莞”城市的url:http://www.weather.com.cn/weather/101281601.shtml, 以及以下类似的天气信息
对网页元素进行“检查”,发现每天的天气信息都存在<p>标签中,分别对应不同的class:wea, tem, win。我们将使用beatifulsoup对这些关键字进行提取,并是哟个pyttsx3进行文字转语音
详细的代码如下:
# coding=utf-8
#!/usr/bin/env python3
# from urllib.request import urlopen
import urllib.request
from bs4 import BeautifulSoup
import re
import pyttsx3
def voice(engine, date, win, temp, weather):
print(date)
print('天气:' + weather)
print('最低温度:' + temp[5:8])
print('最高温度:' + temp[1:4])
print('风级:' + win)
print('n')
engine.say(date)
engine.say('天气:' + weather)
if temp[5:8] != '':
engine.say('最低温度:' + temp[5:8])
if temp[1:4] != '':
engine.say('最高温度:' + temp[1:4])
engine.say('风级小于:' + win[1:4])
engine.runAndWait()
def parse_weather_infor(url):
# add a headers to simulate as a web browser
headers = ("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/61.0.3163.100 Safari/537.36")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
resp = opener.open(url).read()
soup = BeautifulSoup(resp, 'html.parser')
# get current date
tagDate = soup.find('ul', class_="t clearfix")
# dates = tagDate.h1.string
tgs = soup.findAll('h1',tagDate)
dates = tgs[0:7]
for d in range(len(dates)):
print(dates[d].getText())
tagAllTem = soup.findAll('p', class_="tem") # get all weather information:
tagAllWea = soup.findAll('p', class_="wea")
tagAllWin = soup.findAll('p', class_="win")
# pyttsx module initial:
engine = pyttsx3.init()
""" RATE"""
rate = engine.getProperty('rate') # getting details of current speaking rate
print (f"default rate:{rate}, set rate to {175}") #printing current voice rate
engine.setProperty('rate', 175) # setting up new voice rate
##
"""VOLUME"""
volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1)
print (volume) #printing current volume level
engine.setProperty('volume',1.0) # setting up volume level between 0 and 1
## """VOICE"""
## voices = engine.getProperty('voices') #getting details of current voice
## #engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male
## engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for female
# get location
location = soup.find('div', class_='crumbs fl')
text = location.getText()
# now start voicing:
print('以下播报' + str(text.split(">")[2])+ '未来7天天气情况......')
engine.say('以下播报' +str(text.split(">")[2]) + '未来7天天气情况')
engine.runAndWait()
# 3 days later
for k in range(len(dates)):
voice(engine, dates[k].getText(), tagAllWin[k].i.string, tagAllTem[k].getText(), tagAllWea[k].string)
engine.say('天气播报完毕')
engine.runAndWait()
if __name__ =="__main__":
url = 'http://www.weather.com.cn/weather/101281601.shtml' # get city website from ww.weather.com
parse_weather_infor(url)
运行结果,效果还不错:
以下播报
东莞未来7天天气情况......
6日(今天)
天气:阴转小雨
最低温度:3℃
最高温度:20/
风级:<3级
7日(明天)
天气:小雨转阴
最低温度:2℃
最高温度:17/
风级:3-4级转<3级