How to delete child element in BeautifulSoup? - GeeksforGeeks

03 Mar, 2021
Beautifulsoup is a Python library used for web scraping. This powerful python tool can also be used to modify html webpages. This article depicts how beautifulsoup can be employed to delete child element. For this, various methods of the module is used.
Methods Used:
clear(): Tag.clear() deletes the tag from the tree of a given HTML document.
decompose(): Tag.decompose() removes a tag from the tree of a given HTML document, then completely destroys it and its contents.
replace(): Tag.replace() replaces a particular tag with a new tag.
Approach:
Import module.
Scrap data from webpage.
Parse the string scraped to html.
Find the tag whose child element to be deleted.
Use any of the methods: clear(), decompose() or replace().
Print replaced content.
Example 1:
Python3
# importing modulefrom bs4 import BeautifulSoup  markup = """  <!DOCTYPE><html>  <head><title>Example</title></head>  <body>  <div id="parent">    <p>    This is child of div with id = "parent".    <span>Child of "P"</span>  </p>    <div>  Another Child of div with id = "parent".  </div>  </div>      <p>  Piyush  </p>                         </body></html>"""  # parsering string to HTMLsoup = BeautifulSoup(markup, 'html.parser')  # finding tag whose child to be deleteddiv_bs4 = soup.find('div')  # delete the child elementdiv_bs4.clear()  print(div_bs4)
Output:
<div id="parent"></div>
Example 2:
Python3
# importing modulefrom bs4 import BeautifulSoup  markup = """  <!DOCTYPE><html>  <head><title>Example</title></head>  <body>  <div id="parent">    <p>    This is child of div with id = "parent".    <span>Child of "P"</span>  </p>    <div>  Another Child of div with id = "parent".  </div>  </div>      <p>  Piyush  </p>                         </body></html>"""  # parsering string to HTMLsoup = BeautifulSoup(markup, 'html.parser')  # finding tag whose child to be deleteddiv_bs4 = soup.find('div')  # delete the child elementdiv_bs4.decompose()  print(div_bs4)
Output:
<None></None>
Example 3:
Python3
# importing modulefrom bs4 import BeautifulSoup  markup = """  <!DOCTYPE><html>  <head><title>Example</title></head>  <body>  <div id="parent">    <p>    This is child of div with id = "parent".    <span>Child of "P"</span>  </p>    <div>  Another Child of div with id = "parent".  </div>  </div>      <p>  Piyush  </p>                         </body></html>"""  # parsering string to HTMLsoup = BeautifulSoup(markup, 'html.parser')  # finding tag whose child to be deleteddiv_bs4 = soup.find('div')  # delete the child elementdiv_bs4.replaceWith('')  print(div_bs4)
Output:
<div id="parent">
<p>
    This is child of div with id = "parent".
    <span>Child of "P"</span>
</p>
<div>
  Another Child of div with id = "parent".
  </div>
</div>
Picked
Python BeautifulSoup
Python bs4-Exercises
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Defaultdict in Python
Python OOPs Concepts
Python | os.path.join() method
Python | Pandas dataframe.groupby()