How to delete child element in BeautifulSoup?
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.
- 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.
- 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.
<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>
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course