Beautiful Soup Tag | children property
Tag.children property in Beautiful Soup returns a generator used to iterate over the immediate 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><p>Cathy</p></div>"""soup = BeautifulSoup(my_html)
To iterate over all the direct 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
Notice how text nodes are excluded.