Practice navigating nested HTML structures, handling white-space NavigableStrings, and traversing up and down the DOM tree interactively inside Google Colab.
View on GitHubWhen parsing HTML documents, tags are structured hierarchically like a tree. BeautifulSoup provides built-in attributes—.children, .contents, .parent, and .parents—that let you navigate up and down through direct child elements or climb up to root parent containers.
To inspect immediate child nodes within a container tag, use either .children (which returns a generator iterator) or .contents (which returns a Python list of direct children).
content = """<h2>List of web programming languages</h2>
<ul>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</ul>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(content, 'html.parser')
# Iterating over direct child nodes using .children
for child in soup.ul.children:
# Filter out white-space text nodes
if child.name:
print(f"Tag Name: {child.name} | Text: {child.string}")
Tag Name: li | Text: PHP
Tag Name: li | Text: Python
Tag Name: li | Text: JavaScript
To move upwards in the DOM tree from a given tag, use .parent for the immediate parent node, or .parents to iterate through all enclosing parent tags up to the root [document] node.
content = """<body>
<h2>List of web programming languages</h2>
<ul>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</ul>
</body>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(content, 'html.parser')
# Direct parent tag
print("Immediate Parent of <li>:", soup.li.parent.name)
# Traverse all ancestor parent tags up to root
print("\nAll Ancestor Parents:")
for parent in soup.li.parents:
print(f"Parent Tag: {parent.name}")
Immediate Parent of <li>: ul
All Ancestor Parents:
Parent Tag: ul
Parent Tag: body
Parent Tag: [document]
<li> tags of a target <ul> or rows (<tr>) inside a table container..parent or .parents to find the surrounding card or article wrapper ID..children and .contents include newline and space strings (\n). Filter nodes with if child.name: or check isinstance(child, Tag) to isolate actual HTML elements..contents returns a standard Python list residing in memory, whereas .children is a generator designed for memory-efficient iteration..parents, the final item in the iteration is the top-level [document] object (where parent.name is '[document]').Q: What is the difference between .children and .descendants?
A: .children considers only immediate sub-tags (1 level deep), whereas .descendants recurses through all nested child tags, strings, and elements regardless of depth.
Q: How do I get only tag children and ignore plain text/whitespace?
A: You can filter the generator: [child for child in soup.ul.children if child.name is not None].
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.