How to find elements without specific attributes in BeautifulSoup?

To find elements without a specific attribute using BeautifulSoup, we use the attrs parameter of the function find, and we specify the attributes as None.

For example, to find the paragraph element without a class name, we set attrs={"class": None}:

import requests
from bs4 import BeautifulSoup

html_content = '''
<p class="clean-text">A very long clean paragraph</p>
<p class="dark-text">A very long dark paragraph</p>
<p>A very long paragraph without attribute</p>
<p class="light-text">A very long light paragraph</p>
soup = BeautifulSoup(html_content, 'html.parser')

no_class_attribute = soup.find("p", attrs={"class": None})

# Output: <p>A very long paragraph without attribute</p>

Related BeautifulSoup web scraping questions: