NESTED NAVIGATION - Deep tree navigation
Python
#!/usr/bin/env python3
"""NESTED NAVIGATION - Deep tree navigation"""
from bs4 import BeautifulSoup
html = """
<div class="level1">
<div class="level2">
<div class="level3">
<p class="target">Found me!</p>
</div>
</div>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
print("Nested Navigation:")
target = soup.find(class_='target')
print(f" Found: {target.string}")
print(" Path from target:")
current = target
path = []
while current.parent:
if current.name:
path.insert(0, current.name)
current = current.parent
print(f" {' > '.join(path)}")