Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Insert tags or strings immediately before and after specified tags using BeautifulSoup

  • Last Updated : 03 Mar, 2021

BeautifulSoup is a Python library that is used for extracting data out of markup languages like HTML, XML…etc. For example let us say we have some web pages that needed to display relevant data related to some research like processing information such as date or address but that do not have any way to download it, in such cases BeautifulSoup comes handy to us as it helps us in pulling up particular content from HTML page and saves information. BeautifulSoup is an effective tool for web scraping that helps in cleaning and parsing documents that are pulled from the web.

Installation Of Required Libraries:

  • bs4: As BeautifulSoup is not provided by default in python, we need to install it in our machines using the below command with pip.
pip install bs4
  • lxml: lxml is a mature bonding of the pythonic libxml2 and libxlst libraries, with help of ElementTree API, it provides safe and convenient access to those libraries.
pip install lxml 

Functions Used: 

  • tag(): Python implementation for inserting tags or strings before specified tags with BeautifulSoup.
  • insert(): The insert() function in BeautifulSoup is used to insert elements into the tag object, it is similiar like .inert() in a python list.
  • insert_before(): The insert_before() method inserts tags or strings immediately before something else in the parse tree.
  • insert_after(): The insert_after method inserts tags or strings following something else in the given parse tree.

Step-by-step Approach:

  • Firstly, we import BeautifulSoup library using bs4.
  • We assign an attribute to BeautifulSoup along with filling it with the source url for which we are trying to implement our program.
  • We assign a new element in the tag object using new_tag().
  • We assign a string to the tag object to attach our tags to before or after it(as specified).
  • We insert the tag before the string using insert_before() function.

Implementation:

Example 1: Python Implementation for inserting tags or strings before Specified tags with BeautifulSoup.

 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

Python3






# import module
from bs4 import BeautifulSoup
  
# assign URL
s = BeautifulSoup("<b>www.geeksforgeeks.com</b>"
                  "lxml")
  
print("Original Markup:")
print(s.b)
  
# insert tag
tag = s.new_tag("k")
tag.string = "Python"
  
print("\nNew Markup, before inserting the text:")
s.b.string.insert_before(tag)
print(s.b)

Output:

Example 2: Here is another Implementation for inserting tags or strings after Specified tags.

Python3




# import module
from bs4 import BeautifulSoup
  
# assign URL
s = BeautifulSoup("<b>www.geeksforgeeks.com</b>"
                  "lxml")
  
print("Original Markup:")
print(s.b)
  
# insert tag
tag = s.new_tag("k")
tag.string = "Python"
  
print("\nNew Markup, before inserting the text:")
s.b.string.insert_after(tag)
print(s.b)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!