How to find HTML elements by attribute using BeautifulSoup?

Using Python and BeautifulSoup, to find elements by attribute value we can use find and find_all methods or CSS selectors through the select and select_one methods:

import bs4
soup = bs4.BeautifulSoup('<a alt="this is a link">some link</a>')

# to find exact matches:
soup.find("a", alt="this is a link")
# or
soup.find("a", {"alt": "this is a link"})

# to find partial matches we can use regular expressions:
import re
soup.find("a", alt=re.compile("a link", re.I))  # tip: the re.I paramter makes this case insensitive

# or using CSS selectors for exact matches:
soup.select('a[alt="this is a link"]')
# and to find partial matches we can contains matcher `*=`:
soup.select('a[alt*="a link"]')
# or
soup.select('a[alt*="a link" i]')  # tip: the "i" suffix makes this case insensitive

Related Articles

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

Ultimate CSS Selector Cheatsheet for HTML Parsing

Ultimate companion for HTML parsing using CSS selectors. This cheatsheet contains all syntax explanations with interactive examples.

CSS-SELECTORS
DATA-PARSING
Ultimate CSS Selector Cheatsheet for HTML Parsing

Web Scraping With Ruby

Introduction to web scraping with Ruby. How to handle http connections, parse html files for data, best practices, tips and an example project.

RUBY
HTTP
DATA-PARSING
CSS-SELECTORS
XPATH
INTRO
Web Scraping With Ruby

Web Scraping With NodeJS and Javascript

In this article we'll take a look at scraping using Javascript through NodeJS. We'll cover common web scraping libraries, frequently encountered challenges and wrap everything up by scraping etsy.com

NODEJS
HTTP
DATA-PARSING
CSS-SELECTORS
INTRO
Web Scraping With NodeJS and Javascript

Parsing HTML with CSS Selectors

Introduction to using CSS selectors to parse web-scraped content. Best practices, available tools and common challenges by interactive examples.

DATA-PARSING
CSS-SELECTORS
Parsing HTML with CSS Selectors

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