From a tag you can walk up (.parent, .find_parent), down (.children, .descendants), and sideways (.next_sibling, .find_next_sibling). Sibling methods often hit whitespace text nodes — skip those.
Goal
Walk from a link up to nav, and from a list item to its next sibling tag.
Parent
html = """
<nav>
<a href="/nairobi/">Nairobi</a>
</nav>
"""
soup = BeautifulSoup(html, "html.parser")
a = soup.a
print(a.parent.name)
print(a.find_parent("nav") is not None)Next sibling tag
html = """
<ul>
<li>Nairobi</li>
<li>Mombasa</li>
<li>Kisumu</li>
</ul>
"""
soup = BeautifulSoup(html, "html.parser")
first = soup.li
print(repr(first.next_sibling))
nxt = first.find_next_sibling("li")
print(nxt.get_text()).next_sibling is often "\n". find_next_sibling("li") skips text and finds the next <li>.
Children
html = """
<ul>
<li>Nairobi</li>
<li>Mombasa</li>
</ul>
"""
soup = BeautifulSoup(html, "html.parser")
for child in soup.ul.children:
if child.name == "li":
print(child.get_text())Tip
.descendants walks every nested tag, not only direct children. Use it when you do not know how deep the markup goes.