Part One
Searching by Class Name
The class_ argument (note the underscore — Python's built-in class keyword would conflict without it) lets you filter by CSS class name. Pass it to find() or find_all() alongside a tag name and you get only the elements that have that class:
An element can have multiple class names at once — class="panel panel-red" means the element belongs to both the panel group and the panel-red group. BeautifulSoup's class_ argument matches if the specified name is anywhere in the class list, so class_="panel-red" correctly finds elements whose class attribute is "panel panel-red".
class as a keyword for defining new classes. BeautifulSoup uses class_ (with a trailing underscore) as the argument name to avoid the conflict. Do not forget the underscore — class= will raise a SyntaxError.
Part Two
Reading Status from the Class Name
The class name is data, not just a styling hook. When an incident card uses panel-red for active fires and panel-green for contained ones, the class name itself tells you the status. You can read it with the .get("class") method, which returns the class list as a Python list of strings:
.get("class") returns a list like ["panel", "panel-red"]. The loop checks each class name in the list against the status dictionary and sets the status when it finds a match. This is cleaner than hardcoding "panel-red" in every comparison.
tag["class"] (like a dictionary) or with tag.get("class"). The difference: tag["class"] raises a KeyError if the attribute is missing; tag.get("class") returns None instead. Use .get() for any attribute that might be absent.
Part Three
Searching by id
The id attribute uniquely identifies one element on a page. When you know the id, you can jump directly to that element without searching the whole tree. Pass id="the-id" to find():
Once you have the section by id, every subsequent search is scoped to that section. Calling forest_section.find_all("div", class_="panel") returns only the panels inside the forest fires section, not the panel in the urban fires section.
id= when you need to pinpoint one specific element; use class_= when you want to collect a category of elements.
Part Four
CSS Selectors with select()
BeautifulSoup also supports CSS selector syntax through the select() method. If you have ever written CSS stylesheets, you already know these patterns. select() always returns a list; select_one() returns the first match or None.
soup.select("div")— all<div>elementssoup.select(".panel-red")— elements with classpanel-redsoup.select("#forest-fires")— the element with idforest-firessoup.select("div.panel td")—<td>elements inside adiv.panel
select() when the CSS selector syntax is more concise — especially when you need to combine tag and class in one expression like "div.panel-red td". Use find_all() when you prefer explicit keyword arguments. Both are correct.
Part Five
Your Turn — Active Fires Only
Use find_all() with class_="panel-red" to extract only the active incidents from the page below. For each active panel, print the location and start date.
class_= to filter elements by CSS class name; how to read the class list from a tag with .get("class"); how to jump to a unique element with find(id="..."); and how select() lets you use CSS selector syntax for concise targeting. In the next chapter you will go deeper into extracting text and attributes — including link URLs and custom data attributes.
Chapter Navigation
Move between chapters.