程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python tutorial: making web content a voice mp3

編輯:Python

When I found out how to Python When performing text to speech conversion in , I want to know how to apply it to a useful research case . then , I remember I often visit Wikipedia page , These pages cover topics that interest me , But it contains a lot of text that I don't want to read completely . I would rather listen to the content of those pages while doing other things .

therefore , I decided to use Python take Wikipedia Convert pages to audio files .

Import package

Let's start by importing useful packages for this task . The library I'm going to use is for fetching parts bs4 and requests , For regular expressions re , And for text to speech pyttsx3 .

from bs4 import BeautifulSoup
import requests
import re
import pyttsx3 as tts

Extract web content

For grab section , We just need to define one URL And retrieve the text from the corresponding web page .

url = "https://en.wikipedia.org/wiki/Wikipedia" #the web page you want to scrape
# Collecting data from the web page
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")

We want to keep only the text . therefore , We only look for paragraph elements .

paragraphs = soup.findAll("p")

If you print variable paragraphs , You will see a list , This includes the use of variable URL All paragraph elements in the visited web page .

Processing data

When we convert text to speech


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved