Beautiful Soup Tag | contents property
Tag.contents property in Beautiful Soup returns a list that contains all 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 get all the direct child elements and text nodes as a list:
['\n', <p>Alex</p>, '\n', <p>Bob</p>, '\n', <p>Cathy</p>, '\n']
Here, the text nodes are
"\n", which represent a new line.
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.