Beautiful Soup Tag | string property
Start your free 7-days trial now!
In Beautiful Soup, the string property returns a NavigableString
object, which is similar to a typical Python string with additional helper methods and properties to navigate around the HTML document.
To demonstrate how to use the string property, here's a toy HTML document:
my_html = """ <div> <p>Alice</p> <p>Bob</p> <p id="cathy"></p> </div>"""soup = BeautifulSoup(my_html)
Examples
To get the text Alice
:
'Alice'
If you want to convert this to a standard Python string, simply use str(~)
like so:
Case when there are multiple children
When a tag has multiple child elements, then a None
is returned:
None
In our HTML document, the root div
tag contains 3 p
tags, so this is why a None
was returned.
Case when there is no inner content
When a tag is empty, then a None
is returned:
soup.find(id="cathy").string
None
Note that an empty string ""
is not returned.
Converting to standard Python strings
To convert Beautiful Soup's NavigableString to a standard Python string, use the native str(~)
method like so:
Let's compare their types:
The potential for memory leaks when using NavigableString
NavigableString holds a reference to the Beautiful Soup library. This means that the system will not be able to free memory even after you finish using Beautiful Soup if you keep hold of NavigableStrings. In order to ensure that memory leaks like this won't happen, convert the final NavigableString to standard Python strings.