Open In App

string attribute in BeautifulSoup – Python

Last Updated : 25 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<body><b> Hello world </b><body>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the b tag
tag = soup.b
  
# Print the string
print(tag.string)


Output: 

Hello World

Example 2: In this example, we are going to get the type of string.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<body><b> Hello world </b><body>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the b tag
tag = soup.b
  
# Print the type of string
print(type(tag.string))


Output: 

<class 'bs4.element.NavigableString'>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads