Text

get_text, strip, and stripped_strings.

get_text() concatenates visible text. strip=True trims each bit. stripped_strings is an iterator of non-empty pieces — useful when you want a list, not one blob.

Goal

Pull heading text, strip whitespace, and list stripped strings from a list.

get_text

html = """
<div>
  <h1> Nairobi kiosk </h1>
  <p>Open <strong>today</strong>.</p>
</div>
"""
soup = BeautifulSoup(html, "html.parser")
print(repr(soup.h1.get_text()))
print(repr(soup.h1.get_text(strip=True)))
print(soup.p.get_text())
print(soup.p.get_text(separator="|"))

separator joins pieces. Default is "", so Open and today become Opentoday unless you pass a space or the tags already have spaces.

stripped_strings

html = """
<ul>
  <li> Chai </li>
  <li> Mandazi </li>
</ul>
"""
soup = BeautifulSoup(html, "html.parser")
print(list(soup.ul.stripped_strings))
Pitfall

get_text() includes text inside <script> and <style> if those tags are in the tree. decompose them first (Modify chapter) on messy pages.