How to find elements without a specific attribute in BeautifulSoup?

Using Python and Beautifulsoup, to find elements without a specific attribute (like class) we can use find or find_all methods or CSS selectors:

import bs4
soup = bs4.BeautifulSoup("""
<a class="ignore">bad link</a>
<a>good link</a>
""")

soup.find_all("a", class_=None)
["<a>good link</a>]
# or using a lambda function:
soup.find_all("a", class_=lambda value: "ignore" not in value)
# or using regular expression
soup.find_all("a", class_=re.compile(""))

Related Articles

How to Parse Web Data with Python and Beautifulsoup

Beautifulsoup is one the most popular libraries in web scraping. In this tutorial, we'll take a hand-on overview of how to use it, what is it good for and explore a real -life web scraping example.

BEAUTIFULSOUP
DATA-PARSING
PYTHON
How to Parse Web Data with Python and Beautifulsoup

Guide to List Crawling: Everything You Need to Know

In-depth look at list crawling - how to extract valuable data from list-formatted content like tables, listicles and paginated pages.

CRAWLING
BEAUTIFULSOUP
PYTHON
Guide to List Crawling: Everything You Need to Know

Ultimate Guide to JSON Parsing in Python

Learn JSON parsing in Python with this ultimate guide. Explore basic and advanced techniques using json, and tools like ijson and nested-lookup

DATA-PARSING
PYTHON
Ultimate Guide to JSON Parsing in Python

What is Parsing? From Raw Data to Insights

Learn about the fundamentals of parsing data, across formats like JSON, XML, HTML, and PDFs. Learn how to use Python parsers and AI models for efficient data extraction.

DATA-PARSING
PYTHON
AI
What is Parsing? From Raw Data to Insights

Intro to Parsing HTML and XML with Python and lxml

In this tutorial, we'll take a deep dive into lxml, a powerful Python library that allows for parsing HTML and XML effectively. We'll start by explaining what lxml is, how to install it and using lxml for parsing HTML and XML files. Finally, we'll go over a practical web scraping with lxml.

PYTHON
TOOLS
DATA-PARSING
Intro to Parsing HTML and XML with Python and lxml

How to Parse XML

In this article, we'll explain about XML parsing. We'll start by defining XML files, their format and how to navigate them for data extraction.

PYTHON
CSS-SELECTORS
XPATH
DATA-PARSING
How to Parse XML