The Wayback Machine - https://web.archive.org/web/20240717024749/https://www.geeksforgeeks.org/navigablestring-class-python-beautifulsoup/
Open In App

NavigableString class – Python Beautifulsoup

Last Updated : 25 Oct, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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'>


Similar Reads

BeautifulSoup object - Python Beautifulsoup
BeautifulSoup object 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. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object. Syntax: BeautifulS
2 min read
Python BeautifulSoup - find all class
Prerequisite:- Requests , BeautifulSoup The task is to write a program to find all the classes for a given Website URL. In Beautiful Soup there is no in-built method to find all classes. Module needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install
2 min read
BeautifulSoup - Find tags by CSS class with CSS Selectors
Prerequisites: Beautifulsoup Beautifulsoup is a Python library used for web scraping. BeautifulSoup object 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. The BeautifulSoup object represents the parsed docume
2 min read
Find tags by CSS class using BeautifulSoup
BeautifulSoup is a Python library for pulling data out of HTML and XML files. When working with HTML documents, we often need to extract specific elements based on their CSS classes. In this article, we will discuss how to find tags by CSS using BeautifulSoup. We are given an HTML document, we need to find and extract tags from the document using t
6 min read
Beautifulsoup Installation - Python
Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work. The latest Version of Beautifulsoup is v4.9.3 as of now. PrerequisitesPythonPip How to install Beauti
1 min read
Python - Obtain title, views and likes of YouTube video using BeautifulSoup
In this article, we will learn how can we obtain data (like title, views, likes, dislikes etc) from any YouTube video using a Python script. For this task, we are going to use very famous library for web scraping BeautifulSoup and Requests. Modules required and Installation : Requests : Requests allows you to send HTTP/1.1 requests extremely easily
2 min read
Tag Object - Python Beautifulsoup
Tag object 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. Tag object corresponds to an XML or HTML tag in the original document. Further, this object is usually used to extract a tag from the whole HTML docu
2 min read
Scrap books using Beautifulsoup from books.toscrape in Python
Web Scraping is the technique of extracting large amounts of data from websites where the extracted data is saved in a local file on your computer. The simplest form of Web scraping is manually copying and pasting data from a web page into a text file or spreadsheet. Sometimes this is the only solution when the websites set up barriers. But in most
4 min read
Extracting an attribute value with beautifulsoup in Python
Prerequisite: Beautifulsoup Installation Attributes are 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 tag may have any number of attributes. For example, the tag &lt;b class="active"&gt; has an attribute “cl
2 min read
Get tag name using Beautifulsoup in Python
Prerequisite: Beautifulsoup Installation Name property 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. Name object corresponds to the name of an XML or HTML tag in the original document. Syntax: tag.name Para
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg