🚀 We are hiring! See open positions

How to use CSS Selectors in Python?

by scrapecrow Oct 31, 2022

Python has several popular packages that can parse HTML using CSS selectors.
The most popular one is BeautifulSoup which can execute CSS selectors through the select() and select_one() methods:

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
<a>link 1</a>
<a>link 2</a>
""")

print(soup.select_one('a'))
"<a>link 1</a>"
print(soup.select('a'))
["<a>link 1</a>", "<a>link 2</a>"]

Another popular package is parsel (also used by scrapy) which can execute CSS selectors through the css() method:

from parsel import Selector

soup = Selector("""
<a>link 1</a>
<a>link 2</a>
""")

print(soup.css('a').get())
"<a>link 1</a>"
print(soup.css('a').getall())
["<a>link 1</a>", "<a>link 2</a>"]

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

Build a Documentation Chatbot That Works on ANY Website

Build an AI chatbot from any docs site using Scrapfly Crawler API, LangChain, and Streamlit. Works on Cloudflare-protected sites.

PYTHON
AI
RAG
CRAWLING
Build a Documentation Chatbot That Works on ANY Website

LangChain Web Scraping: Build AI Agents & RAG Applications

Learn to integrate LangChain with Scrapfly for web scraping. Build AI agents and RAG applications that extract, process, and understand web data at scale.

AI
PYTHON
LANGCHAIN
SCRAPING
LangChain Web Scraping: Build AI Agents & RAG Applications

Best Web Scraping Tools in 2026

Comprehensive guide to choosing web scraping tools for production. Learn the scraping pipeline framework and how to combine tools like Scrapfly, BeautifulSoup, Playwright, and Scrapy for reliable data extraction at scale.

PYTHON
NODEJS
WEB-SCRAPING
Best Web Scraping Tools in 2026

How to Scrape Facebook: Marketplace and Events

Complete guide to scraping Facebook data including Marketplace listings and Events. Covers authentication, anti-bot bypass, and production-ready techniques.

PYTHON
PLAYWRIGHT
SCRAPEGUIDE
How to Scrape Facebook: Marketplace and Events

Crawl4AI Explained: The AI-Friendly Web Crawling Framework

Discover Crawl4AI, the AI-friendly web crawling framework. Learn features, installation, and intelligent web scraping for LLMs.

WEB-SCRAPING
AI
PYTHON
CRAWLING
Crawl4AI Explained: The AI-Friendly Web Crawling Framework