Find Elements Without a Specific Attribute in BeautifulSoup
BeautifulSoup lets you invert the usual attribute search: instead of asking for elements that have a class or id, you can ask for those that do not.
The key is the attrs argument, where a value of None means the attribute must be absent. This guide shows the pattern with a paragraph example and explains when to use find() versus find_all().
How the None pattern works
Pass a dictionary to attrs in which the attribute you want to exclude is set to None. For example, attrs={'class': None} tells BeautifulSoup to match elements that do not carry a class attribute. The same structure applies to other attributes such as id or style: set the key to None to require its absence. This is different from matching an empty string; None targets elements where the attribute is not present at all.
find() versus find_all()
find() returns the first matching element, while find_all() returns every match. If your HTML contains several paragraphs without a class, find() gives you only the first one. Use find_all() when you need to process all matching elements, for example to extract text from each paragraph in a loop. The attrs dictionary works the same way in both methods.
Example: a paragraph without a class
The code module below builds a small HTML fragment with four paragraphs. Three have a class attribute; one does not. The call soup.find('p', attrs={'class': None}) returns the paragraph without a class, and the printed output confirms the match.
Decision checklist
Before writing the selector, decide:
- Do you need one element or every match? Choose find() for one, find_all() for all.
- Which attribute must be absent? Put that attribute name in the attrs dictionary with a value of None.
- Is the attribute truly missing, or present but empty? None matches absence, not an empty value.
- Do you need to combine the absence check with a tag name? Pass the tag as the first argument, as in find('p', attrs={'class': None}).
Frequently asked questions
Does attrs={'class': None} work with find_all() as well as find()?
Yes. The attrs dictionary is accepted by both methods. find() returns the first matching element, and find_all() returns a list of every element that lacks the specified attribute.
What is the difference between an empty class and a missing class?
A missing class means the attribute is not present on the element. An empty class means the attribute exists but has no value. Setting the attribute to None in attrs targets the missing case, not the empty one.