对于小说,现在的朋友估计都已经看腻了,这就要有声有色的声音来调剂下,虽然小说一直看一直爽。但是小说观看的时间久了,会给我们的眼睛上带来很严重的疲劳视觉,得给我们的眼睛放放假,多一点好听的身喉。比如:d.datouwang.com/uploads/file/yinxiao/2020/yinxiao4395.mp3http://d.datouwang.com/uploads/file/yinxiao/2020/yinxiao4395.mp3
为了解决这个问题,同时能够让大家体验小说的内容,小编今天带领大家爬取小说,并将小说转化成语音形式,让大家以听的形式来“阅读”小说,同时解放自己的双眼。
开发组件python3.6 以上版本就行
requests(爬虫模块)
time(时间模块)
pyttsx3(语音模块)
lxml(解析和提取HTML/XML数据)
pip install requests pip install sys pip install time pip install pyttsx3 pip install lxml基于python的有声小说读模块详细
1.朗读模块
def talkWith(engine, line):
""" 朗读内容 """
engine.say(line)
engine.runAndWait()
def talkContent(line):
""" 朗读字符串内容 使用系统文字转语音 """
engine = pyttsx3.init()
# 设置朗读速度
engine.setProperty('rate', 160)
# 如果字符串过长 通过句号分隔 循环读取
if len(line) > 20:
con_list = line.split('。')
for item in con_list:
time.sleep(1)
talkWith(engine, item)
else:
talkWith(engine, line)
# 打开名为{0}.txt的文件并且读取
content = open('{0}.txt'.format(e), 'r', encoding='utf-8')
line = content.read()
talkContent(line)
print('阅读结束,十秒后关闭')
2.进度条
# 进度条
def progress_bar():
for io in range(1, 101):
print("r", end="")
print("Download progress: {}%: ".format(io), "▋" * (io // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
print()
这一块代码用于实现文件读取进度,这一块没啥好说的,效果如图所示:
阅读完毕后,十秒钟程序结束提醒
# 十秒钟倒计时
def jindut_a():
for i in range(10, 0, -1):
print(i)
time.sleep(1)
(二)
1.分析
进入小说网站
我们先将这些小说先爬取下来,然后添加到文件件中
# coding:utf-8 是为了兼容Emacs的编码声明 开头加上,不然会报错
具体函数如下
# coding:utf-8
import pyttsx3
from lxml import etree
import requests
import time
import sys
# 声名请求头
headers = {
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47'
}
url = "http://www.gebiqu.com/biquge_208216/44952611.html" # /artdetail_32486.html
# URL = parse.urljoin(url, path) # 地址拼接
response = requests.get(url)
response.encoding = "utf-8" # 编码转换
html = response.text # 获取网页源代码
ele = etree.HTML(html)
book_names = ele.xpath("//*[@id='content']/text()") # 标签筛选规则 爬取标签内容
s = ''
def remove_upprintable_chars(t):
return ''.join(x for x in t if x.isprintable())
print("请输入书籍名字下载")
e = input()
for book_name in range(len(book_names)):
s += book_names[book_name] + 'n'
with open('{0}.txt'.format(e), 'w', encoding='utf-8-sig') as file:
file.writelines(s)
print("输入完成")
print('开始下载')
(三)总结
程序结束后会保存一个txt,阅读的内容。
# coding:utf-8
import pyttsx3
from lxml import etree
import requests
import time
import sys
# 声名请求头
headers = {
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47'
}
url = "http://www.gebiqu.com/biquge_208216/44952611.html" # /artdetail_32486.html
# URL = parse.urljoin(url, path) # 地址拼接
response = requests.get(url)
response.encoding = "utf-8" # 编码转换
html = response.text # 获取网页源代码
ele = etree.HTML(html)
# book_neir = ele.xpath("//div[@class='']/div[@class='tablel']/tbody/tr/td/a/@href")
book_names = ele.xpath("//*[@id='content']/text()") # 标签筛选规则 //*[@id="content"]
s = ''
def remove_upprintable_chars(t):
return ''.join(x for x in t if x.isprintable())
print("请输入书籍名字下载")
e = input()
for book_name in range(len(book_names)):
s += book_names[book_name] + 'n'
with open('{0}.txt'.format(e), 'w', encoding='utf-8-sig') as file:
file.writelines(s)
print("输入完成")
print('开始下载')
def progress_bar():
for io in range(1, 101):
print("r", end="")
print("Download progress: {}%: ".format(io), "▋" * (io // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
print()
progress_bar()
print('下载完成,开始阅读')
# 朗读
def talkWith(engine, line):
""" 朗读内容 """
engine.say(line)
engine.runAndWait()
def talkContent(line):
""" 朗读字符串内容 使用系统文字转语音 """
engine = pyttsx3.init()
# 设置朗读速度
engine.setProperty('rate', 160)
# 如果字符串过长 通过句号分隔 循环读取
if len(line) > 20:
con_list = line.split('。')
for item in con_list:
time.sleep(1)
talkWith(engine, item)
else:
talkWith(engine, line)
# 打开名为{0}.txt的文件并且读取
content = open('{0}.txt'.format(e), 'r', encoding='utf-8')
line = content.read()
talkContent(line)
print('阅读结束,十秒后关闭')
# 十秒钟倒计时
def jindut_a():
for i in range(10, 0, -1):
print(i)
time.sleep(1)
jindut_a()
print('程序退出')
看了图,是不是感觉很nice!!! 喜欢就给个关注,点个赞支持一下哦,你的支持是我前进的动力!!!



