Last Updated : 23 Jul, 2025
BeautifulSoup in Python helps in scraping the information from web pages made of HTML or XML. Not only it involves scraping data but also involves searching, modifying, and iterating the parse tree. In this article, we will discuss modifying the content directly on the HTML web page using BeautifulSoup.
Syntax:
Terms Used:old_text = soup.find("#Widget", {"id":"#Id name of widget in which you want to edit"})
old_text.string = re.sub(r'Text you want to edit', 'New Text', old_text.string)
Note: when multiple text node is present one should use .contents, .strings, .children etc. with loops.
Example:
For instance, consider this simple page source.
HTML
<!DOCTYPE html>
<html>
<head>
My First Heading
</head>
<body>
<p id="para">
Geeks For Geeks
</p>
</body>
</html>
Once you have created a driver, you can replace the text 'Geeks For Geeks' with 'Vinayak Rai' using –
Step-by-step Approach:old_text = soup.find("p", {"id": "para"})
old_text.string = re.sub(r'Geeks For Geeks', 'Vinayak Rai', old_text.string)
Step 1: First, import the libraries Beautiful Soup, os and re.
from bs4 import BeautifulSoup as bs
import os
import re
Step 2: Now, remove the last segment of the path.
base=os.path.dirname(os.path.abspath(__file__))
Step 3: Open and Parse the HTML File
with open(os.path.join(base, '#HTML filename you want to edit)) as html_file:
soup = bs(html_file, 'html.parser')
Step 4: Further, give the appropriate location of the text which you wish to replace.
old_text=soup.find("#Widget Name", {"id":"#Id name of widget in which you want to edit"})
Step 5: Next, replace the already stored text with the new text you wish to assign.
old_text.string=re.sub(r'#Text which you want to edit', '#New Text which you want to replace with', old_text.string)
Step 7: Finally, alter the HTML file to see the changes done in the previous step.
Implementation: Pythonwith open("#HTML filename in which you want to store the edited text", "wb") as f_output:
f_output.write(soup.prettify("utf-8"))
# Python program to modify HTML
# with the help of Beautiful Soup
# Import the libraries
from bs4 import BeautifulSoup as bs
import os
import re
# Remove the last segment of the path
base = os.path.dirname(os.path.abspath(__file__))
# Open the HTML in which you want to make changes
with open(os.path.join(base, 'gfg.html')) as html_file:
# Parse HTML file in Beautiful Soup
soup = bs(html_file, 'html.parser')
# Give location where text is stored which you wish to alter
old_text = soup.find("p", {"id": "para"})
# Replace the already stored text with the new text which you wish to assign
old_text.string = re.sub(r'Geeks For Geeks', 'Vinayak Rai', old_text.string)
# Alter HTML file to see the changes done
with open("gfg.html", "wb") as f_output:
f_output.write(soup.prettify("utf-8"))
Output:
how-to-modify-html-using-beautifulsoupRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4