string attribute in BeautifulSoup – Python
string attribute 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. If a tag has only one child, and that child is a NavigableString, the child can be accessed using .string.
Prerequisite: Beautiful Soup Installation.
Syntax:
tag.string
Below given examples explain the concept of string in Beautiful Soup.
Example 1: In this example, we are going to get the string.
Python3
# Import Beautiful Soupfrom bs4 import BeautifulSoup # Create the documentdoc = "<body><b> Hello world </b><body>" # Initialize the object with the documentsoup = BeautifulSoup(doc, "html.parser") # Get the b tagtag = soup.b # Print the stringprint(tag.string) |
Output:
Hello World
Example 2: In this example, we are going to get the type of string.
Python3
# Import Beautiful Soupfrom bs4 import BeautifulSoup # Create the documentdoc = "<body><b> Hello world </b><body>" # Initialize the object with the documentsoup = BeautifulSoup(doc, "html.parser") # Get the b tagtag = soup.b # Print the type of stringprint(type(tag.string)) |
Output:
<class 'bs4.element.NavigableString'>


