VOOZH about

URL: https://www.geeksforgeeks.org/python/beautifulsoup-modifying-the-tree/

⇱ BeautifulSoup - Modifying the tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

BeautifulSoup - Modifying the tree

Last Updated : 23 Jul, 2025

Prerequisites: 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 modify the parse tree. BeautifulSoup is used to search the parse tree and allow you to modify the tree. You can rename tag, change the values of its attributes, add and delete attribute.

Modifying the name of the tag and its attributes

You can change the name of the tag and modify its attribute by adding or deleting them.

  • To change tag name:

Syntax: tag.name = "new_tag"

  • To modify its attribute or to add new attribute:

Syntax: tag["attribute"] = "value"

  • To delete any attribute:

Syntax: del tag["attribute"]

A tree can also be modified by inserting new elements at required places.

  • insert() function will insert new element at any position

Syntax: tag.insert()

  • insert_after() function will insert element after something in the parse tree.

Syntax: tag.insert_after()

  • insert_before() function will insert element before something in the parse tree.

Syntax: tag.insert_before()

Approach :

  • Import module
  • Scrap data from webpage
  • Parse the string scraped to html
  • Select tag within which modification has to be performed
  • Make required changes

Example 1: 

Output:

👁 Image

Example 2:

Output:

👁 modify tree python bs4

Adding new tag and wrapping element 

The tree can be modified by adding a new tag at any required location. We can also wrap the element to modify it.

  • new_tag() function will add a new tag

Syntax: new_tag("attribute")

  • wrap() function will enclose an element in the tag you specify and returns a new wrapper

Syntax: wrap()

  • unwrap() function unwrap the wrapped elements.

Syntax: unwrap()

Example:

Output:

👁 Image

Replacing element

replace_with() function will replace old tag or string with new tag or string in the parse tree.

Syntax: replace_with()

Example:

Output:

<a href="https://www.gfg.com//">Geeks for Geeks <p>gfg.in</p></a>

Adding new content to an existing tag

For adding new contents to an existing tag can be done by append() function or NavigableString() constructor.

Syntax: tag.append("content")

Example:

Output:

<a href="https://www.geeksforgeeks.org/">Geeks for Geeks| A Computer Science portal</a>

<a href="https://www.geeksforgeeks.org/">Geeks for Geeks| A Computer Science portal for geeks</a>

Removing content and element

A tree can be modified by removing content from it or by removing element also.

  • clear() removes the contents of the tag.

Syntax: clear()

  • extract() removes a tag or strings from the tree.

Syntax: extract()

  • decompose() removes the tag and delete it all content.

Syntax: decompose()

Example:

Comment
Article Tags: