實戰(zhàn)項目: 利用urllib爬取小說網(wǎng)站內容后朗讀出來
涉及模塊 pyttsx3,(安裝pip install pyttsx3)
環(huán)境PyCharm+python3.6.3
在項目進行之前,我們首先要配置下自己的環(huán)境,找到發(fā)出聲音的東西。不然,硬件設備不支持,沒用的!
#利用pywin32模塊,來實現(xiàn)微軟的語音接口調用
#安裝pip3 install pywin32
import win32com.client
#微軟這個服務器
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("你好,小姐姐,能加個微信嗎?")
之后運行,就會聽到悅耳的聲音!
然后,我們簡單看一下pyttsx3模塊的用法
import pyttsx3
engine = pyttsx3.init()
engine.say('君不見,黃河之水天上來,奔流到海不復回。')
engine.say('君不見,高堂明鏡悲白發(fā),朝如青絲暮成雪。')
#運行并且等待
engine.runAndWait()
我們看一下pyttsx3.init()的源代碼
image.png
從方法聲明上來看,第一個參數(shù)指定的是語音驅動的名稱,這個在底層適合操作系統(tǒng)密切相關的。如下:
drivename:由pyttsx.driver模塊根據(jù)操作系統(tǒng)類型來調用,默認使用當前操作系統(tǒng)可以使用的最好的驅動
sapi5 - SAPI5 on Windows
nsss - NSSpeechSynthesizer on Mac OS X
espeak - eSpeak on every other platform
debug: 這第二個參數(shù)是指定要不要以調試狀態(tài)輸出,建議開發(fā)階段設置為True
接下來看一下pyttx3的一些常規(guī)操作。
getProperty #獲取當前引擎實例的屬性值
setProperty #設置當前引擎實例的屬性值
#更改聲音(音色處理)
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
print(len(voices))
for voice in voices:
engine.setProperty('voice', voice.id)
engine.say('I will always love you ')
engine.runAndWait()
發(fā)現(xiàn)打印出voices的長度為2,系統(tǒng)默認兩種聲音
image.png
#控制語速(頻率處理)
import pyttsx3
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-150)
engine.say('I will always love you')
engine.runAndWait()
到這里,運行結果聽一下,有沒有一種樹懶的感覺,哈哈
#控制音量
import pyttsx3
engine = pyttsx3.init()
volume = engine.getProperty('volume')
#engine.setProperty('volume', volume-0.25) 不明顯
engine.setProperty('volume', volume-0.75)
engine.say('I will always love you')
engine.runAndWait()
到這里,我們整理一下,對txt文件中的內容進行朗讀
直接上代碼,對文件操作不熟悉的,自行百度
import pyttsx3
with open('2.txt','r',encoding='utf-8') as f:
line = f.read()#文件不大,一次性讀取
engine = pyttsx3.init()
#調整頻率
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-50)
# 調整音量
volume = engine.getProperty('volume')
engine.setProperty('volume', volume+0.25)
engine.say(line)
engine.runAndWait()
朗讀的內容是李白的《將進酒》,不足之處,對多音字沒處理(感興趣的同學可以嘗試做一下容錯)
image.png
好的,到這里,開始今天的實戰(zhàn)項目-----爬取小說,朗讀出來
項目開始之前我們首先要分析:
第一步:爬取小說網(wǎng)站
第二步:利用xpth 獲取內容
第三步:利用文件操作生成一個txt文件
第四步:處理文件內容
第五步:讀取出來
直接上代碼
from urllib import request
import time
import pyttsx3
from lxml import etree
#小說《大醫(yī)凌然》 志鳥村 著
url = 'https://read.qidian.com/chapter/Y8j5WWawoqd1C4AOuV6yIg2/oG-HexlEuhG2uJcMpdsVgA2'
headers = {
"Referer": "https://read.qidian.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
req = request.Request(url=url,headers=headers)
response = request.urlopen(req)
content = response.read().decode()
#復制html中的文本的XPath
##//*[@id="chapter-406583253"]/div/div[2]/p[1]
# print(content)
xpath_content = etree.HTML(content)
new_content = xpath_content.xpath('//*[@id="chapter-406583253"]/div/div/p/text()')
#print(new_content)
with open('3.txt','w',encoding='utf-8') as f:
for i in new_content:
f.writelines(i.strip())
f.writelines('\n')
time.sleep(2)
with open('3.txt','r',encoding='utf-8') as f:
line = f.read()
engine = pyttsx3.init()
volume=engine.getProperty('volume')
engine.setProperty('volume', volume + 0.25)
engine.say(line)
engine.runAndWait()
運行結束,開始朗讀
我們對比下爬取文本的內容和小說內容,發(fā)現(xiàn)一致,
image.png
image.png
感謝閱讀