The NavigableString object
A NavigableString
object holds the text within an HTML or an XML tag. This is a Python Unicode string with methods for searching and navigation. Sometimes we may need to navigate to other tags or text within an HTML/XML document based on the current text. With a normal Python Unicode string, the searching and navigation methods will not work. The NavigableString
object will give us the text within a tag as a Unicode string, together with the different methods for searching and navigating the tree.
We can get the text stored inside a particular tag by using ".string"
.
first_a_string = soup_atag.string
In the previous code, the NavigableString
object (first_a_string
) is created and this holds the string inside the first <a>
tag, u'Home'
.