How to find HTML element by class with BeautifulSoup?

Using Python and BeautifulSoup, to find elements by class name we can either use find and find_all functions with the class_ parameter or CSS selectors:

import bs4
soup = bs4.BeautifulSoup('<a class="social-link">some link</a>')

# using find() and find_all() methods:
soup.find("a", class_="social-link")  # alternatively find_all can be used to find all
soup.find("a", {"class": "social-link"})
# to find by partial class name we can use regex:
import re
soup.find("a", class_=re.compile("link", re.I))  # tip: re.I parameter makes this case insensitive

# using css selectors via select() and select_one() methods
soup.select('.social-link')
# to find by partial class name we can use `*=` matcher:
soup.select('[class*="link"]') 
# or
soup.select('[class*="link" i]')  # "i" addition 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