听书简介:听书不仅可以让我们获得知识,灵感,还可以节省我们的时间。现在听书软件很多,文章不一定是我们喜欢的。自制听书程序,选一本自己喜欢的文章来听听吧。
用以下两种方式来实现:
一:爬虫
获取文章链接和文章名
import requests
from bs4 import BeautifulSoup
import speech
url='http://www.rensheng5.com/zx/onyilin/'
text=requests.get(url).content.decode('gbk')
bs=BeautifulSoup(text,'html.parser')
text_url=0
for u in bs.find_all('a',attrs={'target':'_blank'},text=True):
print(u['href'],u.text)
获得文章,实现听书
url='http://www.rensheng5.com/yilinzazhi/xljt/id-177928.html'
text=requests.get(url,headers={'User-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}).content.decode('gbk')
bs=BeautifulSoup(text,'html.parser')
bt = bs.find_all('meta', attrs={'name': 'keywords'})
bt=bt[0]['content']
tt=bs.find_all('div',attrs={'class':'artinfo'})[0].text
wen=bs.find_all('p')[0].text
speech.say(''.join(wen.split()))
第二种方式是从文件中读取,然后使用speech.say方法
缺点,没有感情,但可以使用pyauio库中的方法来选择自己喜欢的音色
这里用到了爬虫伪装,请求头