How to change tag names and attributes in HTML document using BeautifulSoup
Here, we will learn how to change tag names and attributes in HTML document using BeautifulSoup
Suppose there is a file named ws.html as follows:
<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> <ul style="list-style-type:square"> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> </body> </html>
The output of the following code is:
Now, we wish to change the name of the tag from ul to underline and the type of list from circle to square, so the following code will help.
First, you should import BeautifulSoup in Python:
from bs4 import BeautifulSoup
We will open the file and read it.
doc=open('ws.html', 'r') page=doc.read()
Then, we will parse the HTML document.
soup = BeautifulSoup(page, 'html.parser')
To access the tag from soup:
tag1=soup.ul
CHANGING THE NAME OF TAG:
tag1.name='underline'
In the above code, we have changed the name of the “ul” tag to the “underline” tag.
.name is used to change the name of the tag.
CHANGING THE ATTRIBUTE OF TAG:
tag1['style'] ="list-style-type:square"
tag1[‘style’] is used to change the attribute ‘style’ of that tag.
Now, to see the changes in the data, write the below code:
tag1
OUTPUT:
Leave a Reply