find

The first matching tag, or None.

find returns the first matching tag, or None. Pass a name, then keyword filters such as id= or class_= (with the underscore — class is a Python keyword).

Goal

Find a heading, a paragraph by class, and handle a miss.

First tag

html = """
<html>
  <body>
    <h1>Nairobi kiosk</h1>
    <p class="lead">Open today.</p>
    <p>Chai is in stock.</p>
  </body>
</html>
"""
soup = BeautifulSoup(html, "html.parser")
print(soup.find("h1"))
print(soup.find("p"))
print(soup.find("p", class_="lead"))

find("p") is the first paragraph, not the second.

Missing tags

html = "<p>Kisumu</p>"
soup = BeautifulSoup(html, "html.parser")
missing = soup.find("table")
print(missing)
print(missing is None)

Always check for None before you call .get_text() on a find result.

Pitfall

class_ takes an underscore. find("p", class="lead") is a SyntaxError.