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:
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’)
Terms Used:
- 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-by-step Approach:
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”))
Implementation:
Python
# 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 html = open (os.path.join(base, 'gfg.html' )) # Parse HTML file in Beautiful Soup soup = bs(html, '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 new_text = old_text.find(text = re. compile ( 'Geeks For Geeks' )).replace_with( 'Vinayak Rai' ) # Alter HTML file to see the changes done with open ( "gfg.html" , "wb" ) as f_output: f_output.write(soup.prettify( "utf-8" )) |
Output: