Cheerio
Fast server-side HTML parsing and traversal API that mirrors jQuery selectors without a browser or JavaScript execution.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
npm install cheeriopnpm add cheerioMethods
No methods match this filter.
Fast server-side HTML parsing and traversal API that mirrors jQuery selectors without a browser or JavaScript execution.
Overview
Cheerio implements the jQuery core API over a server-side HTML parser. load returns a selector function, and from there find, each, attr, and text operate on the document tree with familiar semantics and no DOM runtime overhead.
It is the standard tool for scraping and static HTML generation in Node: extracting fields from fetched pages, rewriting links and attributes, and rendering templates from markup fragments. The selection API is read-mostly, but attribute and structure mutations are supported and serializable with $.html().
Core Methods
The most frequently used entry points, with signatures and runnable examples:
cheerio.load— Parses HTML into a queryable document; the returned function selects with CSS selectors.selector / traversal— jQuery-style traversal and attribute access over the parsed tree, returning Cheerio collections.html / text extraction— Serializes the document or a selection back to HTML markup, or extracts concatenated text content.
Usage Notes
- Normalize the input encoding before load when fetching from varied sources; Cheerio assumes UTF-8 unless told otherwise.
- Extract with selectors that match stable semantics, headings or data attributes, instead of brittle nth-child chains.
- Compose Cheerio with a fetcher plus a rate limiter so scraping stays polite and observable.
Common Pitfalls
- Client-rendered pages return empty shells to Cheerio; confirm content exists in the raw HTML before blaming selectors.
- .html() of untrusted pages is not sanitized output; inserting it elsewhere re-introduces XSS unless you sanitize first.
- Text extraction can silently return an empty string when a selector misses, so assert on lengths in scraping pipelines.
FAQ
- Does Cheerio execute page JavaScript?
- No. Cheerio parses markup only. Scripts are ignored and nothing runs, which makes it fast and safe but useless for pages that render content client-side; use a browser automation tool for those.
- How do I scrape pages safely?
- Treat extracted values as untrusted input: never inject .html() output into your own pages without sanitization, respect robots rules and rate limits, and keep selectors defensive so layout changes fail loudly instead of returning empty strings.
- When should I enable xml mode?
- Pass xml: true for XHTML, SVG, and other XML documents so parsing follows XML rules including case sensitivity and self-closing tags. HTML mode normalizes tag case and repairs malformed markup instead.
Report a problem with this API docOpens a pre-filled issue on GitHub.