Beautiful Soup Tag | descendants property
Tag.descendants property in Beautiful Soup returns a generator used to iterate over the all child elements and text nodes (i.e. Navigable String).
Consider the following HTML document:
my_html = """<div id="names"><p>Alex</p><p>Bob</p></div>"""soup = BeautifulSoup(my_html)
To iterate over all the child elements and text nodes:
The awkward spacing is caused by the fact that text nodes, which are line breaks (i.e.
"\n") in this case, are also iterated over.
Most of the time, you just want the elements without the text nodes. You can do this using the
soup.find("div").find_all()[<p>Alex</p>, <p>Bob</p>, <p>Cathy</p>]
Notice how text nodes are excluded.