serp.fast
← Glossary

CSS Selector

A CSS selector is a string syntax for matching elements in an HTML document, originally designed for stylesheets but widely used for DOM querying in scraping. Selectors range from simple (`div`, `.product-title`, `#main`) to compound (`article > h1.headline`, `nav a:not(.disabled)`, `td:nth-child(3)`). Browsers, jQuery, and most scraping libraries (Beautiful Soup, Cheerio, lxml) all implement the same standard, so selector knowledge transfers across tools. For scraping, CSS selectors are usually the first tool to reach for. They are easier to read and write than XPath, well-supported across libraries, and sufficient for the majority of extraction tasks: pull all elements of class X, get the href of the first link in the navigation, extract the text from the third table cell. Modern selector implementations support attribute matching (`[data-product-id]`), pseudo-classes (`:nth-of-type`), and combinators (`>`, `+`, `~`). For AI builders, selector-based scraping is brittle to site redesigns — when the target changes its CSS class names, your selectors stop working overnight. The robust answer is either to monitor and update selectors continuously (works at small scale) or to use LLM-based extraction (works at larger scale but costs more per page). The pragmatic middle ground is to write selectors with stable anchors (semantic HTML, data attributes, ARIA roles) and to maintain a test suite that catches breakage early.