Thursday, September 4, 2025
HomeLanguagesConverting HTML to Text with BeautifulSoup

Converting HTML to Text with BeautifulSoup

Many times while working with web automation we need to convert HTML code into Text. This can be done using the BeautifulSoup. This module provides get_text() function that takes HTML as input and returns text as output.

Example 1:

Python3




# importing the library
from bs4 import BeautifulSoup
 
# Initializing variable
gfg = BeautifulSoup("<b>Section </b><br/>BeautifulSoup<ul>\
<li>Example <b>1</b></li>")
 
# Calculating result
res = gfg.get_text()
 
# Printing the result
print(res)


 

 

Output:

 

Section BeautifulSoupExample 1

 

Example 2: This example extracts data from the live website then converts it into text. In this example, we used the request module from urllib library to read HTML data from URL.

 

Python3




# importing the library
from bs4 import BeautifulSoup
from urllib import request
 
# Initializing variable
gfg = BeautifulSoup(request.urlopen(url).read())
 
# Extracting data for article section
bodyHtml = gfg.find('article', {'class' : 'content'})
 
# Calculating result
res = bodyHtml.get_text()
 
# Printing the result
print(res)


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS