每日新闻人声朗读器

做这个东西的初衷就是每天早上准备开始干活之前可以关注一下国内外的安全消息,同时最好是有声的。

但是出现了一个问题,😂网页改版了。 

整体实现流程

url:https://www.securityweek.com/

注:需要开代理

1.python爬虫获取安全头条,同时捕捉具体内容的url

from bs4 import BeautifulSoup
import requests
import get_ch

header = {}
def geturl():
    url="https://www.securityweek.com"
    html=requests.get(url=url)
    soup=BeautifulSoup(html.text,features="html.parser")
    conturls=[]
    for i in range(1,6):
        selectresult=soup.select(".panel-pane.pane-block.pane-views-lastest-security-block-1")[0].findAll("a")[i].attrs["href"]
        conturl=url+selectresult
        #print(conturl)
        conturls.append(conturl)
    return conturls

def getcontent(url):
    header={}
    html=requests.get(url=url)
    #print(html.text)
    soup=BeautifulSoup(html.text,features="html.parser")
    contents=["title"]
    for content in soup.select("div.content.clear-block")[0].findAll("p"):
        #print(content.text)
        contents.append(content.text)
    return contents



for url in geturl():
    #加入多线程
    print(url)
    #print(getcontent(url))

优化建议:加入多线程(这里优化的意义其实不大,因为量少) ,本来是提醒自己要做的,但是放假了/doge,忘了,回来以后发现网页改版了😭,需要重写部分代码。懒得搞了。等找到好一点的语音合成再做。

2.bs4库解析获取内容

import requests
from bs4 import BeautifulSoup

def getcontent(url):
    header={}
    html=requests.get(url=url)
    #print(html.text)
    soup=BeautifulSoup(html.text,features="html.parser")
    contents=["title"]
    for content in soup.select("div.content.clear-block")[0].findAll("p"):
        #print(content.text)
        contents.append(content.text)
    return contents


if __name__=="__main__":
    #调用列表进行操作
    url = "https://www.securityweek.com/xdr-and-age-old-problem-alert-fatigue"
    print(getcontent(url)[6])

这一段当时放在了第一步,但是后来想着做通用型的,又把它拆开了。。。 

3.调用百度的翻译api进行翻译

import requests
import hashlib
import json
from getthings import *
import re

def md5(str):
    #MD5加密
    # 待加密信息
    # 创建md5对象
    hl = hashlib.md5()
    hl.update(str.encode(encoding='utf-8'))
    # print('MD5加密前为 :' + str)
    # print('MD5加密后为 :' + hl.hexdigest())
    sign=hl.hexdigest()
    return sign


def trans(content):
    '''
         q=要翻译的东西
         from=en
         to=zh
         appid=xxxxxx
         salt=xxxxxx
         平台分配的密钥:xxxxxxxx

    '''
    q=content
    get_from="en"
    to="zh"
    appid="xxxxxxx"
    salt="xxxxx"
    key="xxxxxxx"

    str=appid+q+salt+key
    sign=md5(str)
    #print(sign)
    url="http://api.fanyi.baidu.com/api/trans/vip/translate?q="+q+"&from=en&to=zh&appid="+appid+"&salt="+salt+"&sign="+sign
    #print(len(url))
    html=requests.get(url=url)
    #print(html.text)
    content_ch=html.json()['trans_result'][0]["dst"]

    return content_ch

def trans_page(url):
    cout=0
    content_chs=[]
    for content_en in getcontent(url):
        cout+=1
        try:
            print(trans(content_en))
            content_ch=trans(content_en)
            content_chs.append(content_ch)
        except KeyError:
            content_en = re.sub(string=content_en, pattern="&", repl="")
            print(trans(content_en))
            content_ch=trans(content_en)
            content_chs.append(content_ch)
            # print(cout)
            # print("_________error________________\n")
    return content_chs

if __name__=="__main__":
    # url = "https://www.securityweek.com/xdr-and-age-old-problem-alert-fatigue"
    # cout = 0
    # for content_en in getcontent.getcontent(url):
    #     cout += 1
    #     try:
    #         print(trans(content_en))
    #     except KeyError:
    #         content = re.sub(string=content_en, pattern="&", repl="")
    #         # print(cout)
    #         # print("_________error________________\n")

    #获取资源并且转换为中文
    cout=0
    for url in geturl():
        cout+=1
        print(trans_page(url))
        with open(str(cout)+'.txt', 'a', encoding='utf-8') as f:
            for con in trans_page(url):
                print(con)
                f.write(con)
            f.close()



这里按照百度的api文档进行调用操作就可以了,有一个符号坑了我半个小时,就是&记得替换掉。

4.使用pyttsx3进行语音合成(实话,效果不咋好。)

import pyttsx3

# 初始化获取语音引擎
speaker = pyttsx3.init()
def get_mp3(text,i):
    # 提取文本
    #text ="这就很ok"
    # 去掉文本中的换行符
    cleaned_text = text.strip().replace('\n',' ')
    # 调整人声类型
    # voices = speaker.getProperty('voices')
    # speaker.setProperty('voice', voices[5].id)
    # 调整语速
    rate = speaker.getProperty('rate')
    speaker.setProperty('rate', 200)
    # 调整声量
    volume = speaker.getProperty('volume')
    speaker.setProperty('volume', 1)
    # 将格式为mp3的音频保存
    speaker.save_to_file(cleaned_text,'./paper'+str(i)+'.mp3')
    speaker.runAndWait()
    speaker.stop()

#获取名字
for i in range(1,6):
        with open(str(i)+".txt","r") as f:
            text=f.read()
            get_mp3(text,i)
            print(text)
            f.close()

朗读中文我换了好几个人声都不行,好像最后只有默认的可以读中文。。。

5.上传mp3到服务器

两个办法

1.sshpass 

2.密钥文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值