How to Handle duplicate attributes in BeautifulSoup ?
Last Updated :
07 Apr, 2021
Sometimes while obtaining the information, are you facing any issue in handling the information received from duplicate attributes of the same tags? If YES, then read the article and clear all your doubts.
Once you have created the list to store the items, write the given below code.
Syntax:
list=soup.find_all(“#Widget Name”, {“id”:”#Id name of widget in which you want to edit”})
After writing the following code, remove the attributes from the output and print the certain item you want from the list.
Approach:
- Import module
- Now, remove the last segment of the path by entering the name of Python file in which you are currently working.
Syntax:
base=os.path.dirname(os.path.abspath(‘#Name of Python file in which you are currently working’))
- Then, open the HTML file from which you want to read the value.
Syntax:
html=open(os.path.join(base, ‘#Name of HTML file from which you wish to read value’))
- Parse the HTML file in BeautifulSoup.
- Further, create a list to store all the item values of the same tag and attributes.
- Next, find all the items which have same tag and attributes.
Syntax:
list=soup.find_all(“#Widget Name”, {“id”:”#Id name of widget in which you want to edit”})
- Later on, remove all the attributes from the tag.
- Finally, print the certain item of the widget tag.
Webpage in use:
HTML
<!DOCTYPE html>
< html >
< head >
Geeks For Geeks
</ head >
< body >
< div >
< p id = "vinayak" >King</ p >
< p id = "vinayak" >Prince</ p >
< p id = "vinayak" >Queen</ p >
</ div >
< p id = "vinayak" >Princess</ p >
</ body >
</ html >
|
Program:
Python
from bs4 import BeautifulSoup as bs
import os
base = os.path.dirname(os.path.abspath( "gfg4.py" ))
html = open (os.path.join(base, 'gfg.html' ))
soup = bs(html, 'html.parser' )
list = [ 3 ]
list = soup.div.find_all( "p" , { "id" : "vinayak" })
for i in list :
i.attrs = {}
print ( list [ 1 ])
print ( list [ 2 ])
|
Output:
<p>Prince</p>
<p>Queen</p>
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...