Finding elements that contain a specific text in Beautiful Soup
Start your free 7-days trial now!
Consider the following HTML document:
my_html = """<p>Alex Bob Cathy</p><p>Chris</p>"""soup = BeautifulSoup(my_html)
To find all tags that contain the substring
Let's break down what we're doing here. Here's the first line:
We're passing in an anonymous function as the sole argument. Explicitly, this is equivalent to the following:
foo function is called for every tag in the HTML document, and only those that return a
True is included in the result.
Here's the first criteria we set:
This ensure that the tag does not contain any child elements. Without this criteria, the top-level tag would also be returned as it obviously includes the text "Bob".