Skip to content
Related Articles

Related Articles

Improve Article

NavigableString class – Python Beautifulsoup

  • Last Updated : 25 Oct, 2020

NavigableString class is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. A string corresponds to a bit of text within a tag. Beautiful Soup uses the NavigableString class to contain these bits of text.

Syntax: 

<tag> String here </tag>

Below given examples explain the concept of NavigableString class in Beautiful Soup. 
Example 1: In this example, we are going to see the type of a string.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Initialize the object with a HTML page
soup = BeautifulSoup('''
    <html>
        <h2> Heading 1 </h2>
        <h1> Heading 2 </h1>
    </html>
    ''', "lxml")
  
# Get the whole h2 tag
tag = soup.h2
  
# Get the string inside the tag
string = tag.string
  
# Print the type
print(type(string))

Output: 



<class 'bs4.element.NavigableString'>

Example 2: In this example we are going to convert the NavigableString to a Unicode string.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Initialize the object with a HTML page
soup = BeautifulSoup('''
    <html>
        <h2> Heading 1 </h2>
        <h1> Heading 2 </h1>
    </html>
    ''', "lxml")
  
# Get the whole h2 tag
tag = soup.h2
  
# Get the string inside the tag and convert 
# it into string
string = str(tag.string)
  
# Print the type
print(type(string))

Output: 

<type 'str'>

 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :