Navigating by Tags
The fastest way to reach an element is by tag name: the tag name becomes an attribute of its parent. This works as long as the tag exists and is unique enough for your purposes.
Tag Name as Attribute
from bs4 import BeautifulSoup
html = "<html><head><title>Page</title></head><body><h1>H</h1><a href="x">link</a></body></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup.title) # <title>Page</title>
print(soup.title.text) # Page
print(soup.h1) # <h1>H</h1>
print(soup.head.title) # nested access works too
What If the Tag Is Missing?
If the tag does not exist, attribute access raises AttributeError instead of returning a friendly None:
try:
print(soup.nosuchtag)
except AttributeError:
print("tag not found")
find for Safe Lookups
For tags that may be missing, prefer find, which returns None on failure:
tag = soup.find("nosuchtag")
print(tag) # None
tag = soup.find("a")
print(tag.get_text()) # link
Multiple Tags with the Same Name
Attribute access always returns the first occurrence in document order. If the page has several links, soup.a is the first one; use find_all("a") when you need all of them.
Summary
You can navigate directly with tag names as attributes (soup.title, soup.head.title), which is concise but raises AttributeError when the tag is absent and only returns the first match. Use soup.find("tag") when the element might be missing, and find_all when you need every occurrence.