首先下载pyttsx3包:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyttsx3
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyttsx3
python代码如下:
import pyttsx3
import io
import sys
engine = pyttsx3.init()
engine.setProperty('voice', 'zh') #开启支持中文
line = "你好,世界!" #要播报的内容
engine.say(line)
engine.runAndWait()
f.close()
使用语音播报天气,代码如下:
import requests #向网址发起网络请求获取网页数据
import pyttsx3
from lxml import etree
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'}
#1.获取实时的天气信息 从天气网站中获取
url = 'https://www.tianqi.com/beijing/'
#2.怎么从网站中获取天气? 爬虫下载天气数据
response = requests.get(url,headers=headers)
#3.筛选出我需要的天气信息 xpath筛选工具
html = etree.HTML(response.text) #数据打包解析
weather_list=html.xpath('//dl[@class="weather_info"]//text()')
weather_text=''.join(weather_list)
print(weather_text)
#4.播报出得到的天气信息 智能语音播报库
weather = pyttsx3.init() #创建一个可以说话的对象 起名叫weather
weather.setProperty('voice', 'zh') #开启支持中文
weather.say(weather_text) #设置说话的内容
weather.runAndWait() #开始说话
参考链接:
-
[1] https://blog.csdn.net/qq_25662827/article/details/122411002?spm=1001.2101.3001.6650.4&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4.pc_relevant_default&utm_relevant_index=7
-
[2] https://blog.csdn.net/L6666688888/article/details/115054198?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3