How to modify HTML using BeautifulSoup ? - GeeksforGeeks

15 Mar, 2021
BeautifulSoup in Python helps in scraping the information from web pages made of HTML or XML. Not only it involves scrapping 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:
old_text=soup.find(“#Widget”, {“id”:”#Id name of widget in which you want to edit”})
new_text=old_text.find(text=re.compile(‘#Text which you want to edit’)).replace_with(‘#New text which you want to replace with’)
Widget: Here, widget stands for the particular widget in which the text you wish to replace from the website is currently stored.
Id Name: Here, Id Name stands for the name you have given to the Id of the particular widget in which text is stored.
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 –
old_text=soup.find(“p”, {“id”:”para”})
new_text=old_text.find(text=re.compile(‘Geeks For Geeks’)).replace_with(‘Vinayak Rai’)
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: Then, open the HTML file in which you wish to make a change.
html=open(os.path.join(base, ‘#Name of HTML file in which you want to edit’))
Step 4: Moreover, parse the HTML file in Beautiful Soup.
soup=bs(html, ‘html.parser’)
Step 5: 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 6: Next, replace the already stored text with the new text you wish to assign.
new_text=old_text.find(text=re.compile(‘#Text which you want to edit’)).replace_with(‘#New Text which you want to replace with’)
Step 7: Finally, alter the HTML file to see the changes done in the previous step.
with open(“#Name of HTML file in which you want to store the edited text”, “wb”) as f_output:
   f_output.write(soup.prettify(“utf-8”))
Python
# Python program to modify HTML# with the help of Beautiful Soup  # Import the libraries from bs4 import BeautifulSoup as bsimport osimport re  # Remove the last segment of the pathbase = os.path.dirname(os.path.abspath(__file__))  # Open the HTML in which you want to make changeshtml = open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soupsoup = bs(html, 'html.parser')  # Give location where text is # stored which you wish to alterold_text = soup.find("p", {"id": "para"})  # Replace the already stored text with # the new text which you wish to assignnew_text = old_text.find(text=re.compile(    'Geeks For Geeks')).replace_with('Vinayak Rai')  # Alter HTML file to see the changes donewith open("gfg.html", "wb") as f_output:    f_output.write(soup.prettify("utf-8"))
Output:
Picked
Python BeautifulSoup
Python bs4-Exercises
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()