Skip to content
On this page

A CSS selector is a pattern that picks out specific elements in an HTML document by matching their tag, class, id, attributes, or position. Originally built to apply styles in CSS, selectors are now the most common way scrapers locate the data they want on a page. .price matches every element with class "price"; #main matches the element with id "main"; div.product > a[href] matches links that are direct children of a product div. Parsing libraries use the exact same selector syntax browsers do.

Quick facts

Targets byTag, class (.), id (#), attribute ([ ]), and position
Comes fromCSS styling - reused for element selection in scraping
Used inBeautifulSoup (select), Playwright, Puppeteer, Cheerio, Scrapy
Combinatorsdescendant (space), child (>), sibling (+, ~)
AlternativeXPath - more powerful, more verbose

CSS selector syntax

Selectors are built from a few primitives. A bare tag name (a, div, h2) matches elements of that type. A dot prefixes a class (.product), a hash prefixes an id (#header), and square brackets match attributes ([data-id] for presence, a[href^="https"] for a value that starts with "https"). You combine these to narrow the match: div.card h3 means an h3 anywhere inside a div with class "card", while div.card > h3 means an h3 that is a direct child. Sibling combinators (+ for the next element, ~ for any later sibling) and pseudo-classes (:first-child, :nth-child(2)) handle position. Chaining without a space (a.btn.primary) requires all conditions on one element.

How scrapers use CSS selectors

Extraction is two steps: fetch the HTML, then query it with selectors. In Python, BeautifulSoup's select() and select_one() take a CSS selector and return matching elements; Scrapy and parsel offer .css(); in Node, Cheerio mirrors jQuery's $('.price'); and browser automation tools like Playwright and Puppeteer use selectors to both find and interact with elements (page.click('button.submit')). The skill is writing selectors that are specific enough to grab the right data but loose enough to survive small markup changes - anchoring on a stable class or data attribute rather than a brittle chain of div > div > div that breaks the moment the layout shifts.

CSS selectors vs XPath

CSS selectors and XPath are the two query languages for HTML, and most tools support both. CSS selectors are shorter and more readable for the common cases - selecting by class, id, or attribute - which is why they are the default for most scraping. XPath is more powerful: it can walk back up to a parent, select an element by its text content, or apply functions, none of which plain CSS can do. A practical rule: reach for CSS first because it is cleaner, and drop to XPath when you need to match on text or navigate upward through the tree. Getting selectors right matters more than which language you pick - and a parsing layer that returns structured fields, like an autoparse step, removes the need to hand-write selectors for common page types.

Code example

python
from bs4 import BeautifulSoup

html = '<div class="product"><a href="/p/1">Widget</a>' \
       '<span class="price">$19.99</span></div>'
soup = BeautifulSoup(html, 'html.parser')

name  = soup.select_one('div.product > a').get_text()   # 'Widget'
price = soup.select_one('.price').get_text()             # '$19.99'
link  = soup.select_one('a[href^="/p/"]')['href']        # '/p/1'

print(name, price, link)

Next in Extracting and exporting data · 3 of 7

The more powerful alternative for awkward documents.

What Is an XPath Selector?

Related terms

Concept map

Concept map

How CSS Selector connects

The terms most directly tied to this one. Hover a node to see its neighbours, click to preview, drag to rearrange.

0 terms · 0 connections
You are here · Web Scraping APIs
Building map…

Frequently asked questions

What is the difference between a CSS selector and XPath?

Both locate elements in HTML. CSS selectors are shorter and read more cleanly for selecting by tag, class, id, or attribute. XPath is more powerful - it can select by text content, navigate to a parent element, and use functions - but it is more verbose. Most scrapers use CSS by default and XPath when they need its extra reach.

How do I select an element by class with a CSS selector?

Prefix the class name with a dot: '.price' matches every element with class 'price'. To require a tag too, write 'span.price'. To require multiple classes on one element, chain them with no space: '.btn.primary' matches elements that have both classes.

Which scraping libraries support CSS selectors?

Most of them: BeautifulSoup (select/select_one), Scrapy and parsel (.css()), Cheerio in Node, and browser automation tools like Playwright, Puppeteer, and Selenium. The syntax is the same one browsers use, so a selector you test in DevTools works in your scraper.

What makes a CSS selector robust?

Anchor on stable hooks - a meaningful class name, an id, or a data attribute - rather than a long chain of generic tags like 'div > div > div', which breaks the moment the layout changes. Specific enough to match only your target, loose enough to survive minor markup edits.

Last updated: 2026-06-08