Beautiful Soup - stripped_strings Property



Method Description

The stripped_strings property of a Tag/Soup object gives the return similar to strings property, except for the fact that the extra line breaks and whitespaces are stripped off. Hence, it can be said that the stripped_strings property results in a generator of NavigableString objects of the inner elements belonging to the object in use.

Syntax

Tag.stripped_strings

Example 1

In the example below, the strings of all the elements in the document tree parsed in a BeautifulSoup object are displayed after applying the stripping.

from bs4 import BeautifulSoup, NavigableString

markup = '''
   <div id="Languages">
      <p>Java</p> <p>Python</p> <p>C++</p>
   </div>
'''
soup = BeautifulSoup(markup, 'html.parser')
print ([string for string in soup.stripped_strings])

Output

['Java', 'Python', 'C++']

Compared to the output of strings property, you can see that the line breaks and whitespaces are removed.

Example 2

Here we extract the NavigableStrings of each of the child elements under the <div> tag.

tag = soup.div

navstrs = tag.stripped_strings
for navstr in navstrs:
   print (navstr)

Output

Java
Python
C++
Advertisements