cheerio
- Version 1.0.0
- Published
- 1.25 MB
- 11 dependencies
- MIT license
Install
npm i cheerio
yarn add cheerio
pnpm add cheerio
Overview
The fast, flexible & elegant library for parsing and manipulating HTML and XML.
Index
Functions
Classes
Interfaces
Type Aliases
Functions
function contains
contains: (container: AnyNode, contained: AnyNode) => boolean;
Checks to see if the
contained
DOM element is a descendant of thecontainer
DOM element.Static
Parameter container
Potential parent node.
Parameter contained
Potential child node.
Returns
Indicates if the nodes contain one another. Cheerio.contains
See Also
function decodeStream
decodeStream: ( options: DecodeStreamOptions, cb: (err: Error | null | undefined, $: CheerioAPI) => void) => Writable;
Parses a stream of buffers into a document.
The stream is a
Writable
stream that accepts buffers. When the stream is finished, the callback is called with the loaded document.Loading
Parameter options
The options to pass to Cheerio.
Parameter cb
The callback to call when the stream is finished.
Returns
The writable stream.
function fromURL
fromURL: ( url: string | URL, options?: CheerioRequestOptions) => Promise<CheerioAPI>;
fromURL
loads a document from a URL.By default, redirects are allowed and non-2xx responses are rejected.
Loading
Parameter url
The URL to load the document from.
Parameter options
The options to pass to Cheerio.
Returns
The loaded document.
Example 1
import * as cheerio from 'cheerio';const $ = await cheerio.fromURL('https://example.com');
function load
load: ( content: string | AnyNode | AnyNode[] | Buffer, options?: CheerioOptions | null, isDocument?: boolean) => CheerioAPI;
Create a querying function, bound to a document created from the provided markup.
Note that similar to web browser contexts, this operation may introduce
<html>
,<head>
, and<body>
elements; setisDocument
tofalse
to switch to fragment mode and disable this.Loading
Parameter content
Markup to be loaded.
Parameter options
Options for the created instance.
Parameter isDocument
Allows parser to be switched to fragment mode.
Returns
The loaded document.
See Also
https://cheerio.js.org#loading for additional usage information.
function loadBuffer
loadBuffer: (buffer: Buffer, options?: DecodeStreamOptions) => CheerioAPI;
Sniffs the encoding of a buffer, then creates a querying function bound to a document created from the buffer.
Loading
Parameter buffer
The buffer to sniff the encoding of.
Parameter options
The options to pass to Cheerio.
Returns
The loaded document.
Example 1
import * as cheerio from 'cheerio';const buffer = fs.readFileSync('index.html');const $ = cheerio.fromBuffer(buffer);
function merge
merge: <T>( arr1: Writable<ArrayLike<T>>, arr2: ArrayLike<T>) => ArrayLike<T> | undefined;
$.merge().
Static
Parameter arr1
First array.
Parameter arr2
Second array.
Returns
arr1
, with elements ofarr2
inserted. Cheerio.mergeSee Also
function stringStream
stringStream: ( options: CheerioOptions, cb: (err: Error | null | undefined, $: CheerioAPI) => void) => Writable;
Creates a stream that parses a sequence of strings into a document.
The stream is a
Writable
stream that accepts strings. When the stream is finished, the callback is called with the loaded document.Loading
Parameter options
The options to pass to Cheerio.
Parameter cb
The callback to call when the stream is finished.
Returns
The writable stream.
Example 1
import * as cheerio from 'cheerio';import * as fs from 'fs';const writeStream = cheerio.stringStream({}, (err, $) => {if (err) {// Handle error}console.log($('h1').text());// Output: Hello, world!});fs.createReadStream('my-document.html', { encoding: 'utf8' }).pipe(writeStream,);
Classes
class Cheerio
abstract class Cheerio<T> implements ArrayLike<T> {}
The cheerio class is the central class of the library. It wraps a set of elements and provides an API for traversing, modifying, and interacting with the set.
Loading a document will return the Cheerio class bound to the root element of the document. The class will be instantiated when querying the document (when calling
$('selector')
).Example 1
This is the HTML markup we will be using in all of the API examples:
<ul id="fruits"><li class="apple">Apple</li><li class="orange">Orange</li><li class="pear">Pear</li></ul>
constructor
constructor( elements: ArrayLike<T>, root: Cheerio<Document>, options: InternalOptions);
Instance of cheerio. Methods are specified in the modules. Usage of this constructor is not recommended. Please use
$.load
instead.Parameter elements
The new selection.
Parameter root
Sets the root node.
Parameter options
Options for the instance.
property length
length: number;
property options
options: InternalOptions;
property prevObject
prevObject: Cheerio<any>;
Interfaces
interface Cheerio
interface Cheerio<T> extends MethodsType, Iterable<T> {}
interface CheerioAPI
interface CheerioAPI extends StaticType {}
A querying function, bound to a document created from the provided markup.
Also provides several helper methods for dealing with the document as a whole.
property fn
fn: typeof Cheerio.prototype;
Mimic jQuery's prototype alias for plugin authors.
property load
load: ReturnType<typeof getLoad>;
The
.load
static method defined on the "loaded" Cheerio factory function is deprecated. Users are encouraged to instead use theload
function exported by the Cheerio module.Example 1
const $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');Deprecated
Use the
load
function exported by the Cheerio module. Deprecated
call signature
<T extends AnyNode, S extends string>( selector?: S | BasicAcceptedElems<T>, context?: BasicAcceptedElems<AnyNode> | null, root?: BasicAcceptedElems<Document>, options?: CheerioOptions): Cheerio<S extends SelectorType ? Element : T>;
This selector method is the starting point for traversing and manipulating the document. Like jQuery, it's the primary method for selecting elements in the document.
selector
searches within thecontext
scope, which searches within theroot
scope.Parameter selector
Either a selector to look for within the document, or the contents of a new Cheerio instance.
Parameter context
Either a selector to look for within the root, or the contents of the document to query.
Parameter root
Optional HTML document string.
Example 1
$('ul .pear').attr('class');//=> pear$('li[class=orange]').html();//=> Orange$('.apple', '#fruits').text();//=> AppleOptionally, you can also load HTML by passing the string as the selector:
$('<ul id="fruits">...</ul>');Or the context:
$('ul', '<ul id="fruits">...</ul>');Or as the root:
$('li', 'ul', '<ul id="fruits">...</ul>');
interface CheerioOptions
interface CheerioOptions extends Parse5ParserOptions<Htmlparser2TreeAdapterMap> {}
Options accepted by Cheerio.
Please note that parser-specific options are _only recognized_ if the relevant parser is used.
property baseURI
baseURI?: string | URL;
The base URI for the document. Used to resolve the
href
andsrc
props.
property pseudos
pseudos?: SelectOptions['pseudos'];
Extension point for pseudo-classes.
Maps from names to either strings of functions.
- A string value is a selector that the element must match to be selected. - A function is called with the element as its first argument, and optional parameters second. If it returns true, the element is selected.
Example 1
const $ = cheerio.load('<div class="foo"></div><div data-bar="boo"></div>',{pseudos: {// `:foo` is an alias for `div.foo`foo: 'div.foo',// `:bar(val)` is equivalent to `[data-bar=val s]`bar: (el, val) => el.attribs['data-bar'] === val,},},);$(':foo').length; // 1$('div:bar(boo)').length; // 1$('div:bar(baz)').length; // 0
property quirksMode
quirksMode?: SelectOptions['quirksMode'];
Is the document in quirks mode?
This will lead to
.className
and#id
being case-insensitive.false
property xml
xml?: HTMLParser2Options | boolean;
Recommended way of configuring htmlparser2 when wanting to parse XML.
This will switch Cheerio to use htmlparser2.
false
property xmlMode
xmlMode?: boolean;
Enable xml mode, which will switch Cheerio to use htmlparser2.
Deprecated
Please use the
xml
option instead. false
interface CheerioRequestOptions
interface CheerioRequestOptions extends DecodeStreamOptions {}
property requestOptions
requestOptions?: UndiciStreamOptions;
The options passed to
undici
'sstream
method.
interface DecodeStreamOptions
interface DecodeStreamOptions extends CheerioOptions {}
property encoding
encoding?: SnifferOptions;
interface HTMLParser2Options
interface HTMLParser2Options extends DomHandlerOptions, HTMLParser2ParserOptions {}
Options accepted by htmlparser2, the default parser for XML.
See Also
https://github.com/fb55/htmlparser2/wiki/Parser-options
Type Aliases
type AcceptedElems
type AcceptedElems<T extends AnyNode> = | BasicAcceptedElems<T> | ((this: T, i: number, el: T) => BasicAcceptedElems<T>);
Elements that can be passed to manipulation methods, including functions.
type AcceptedFilters
type AcceptedFilters<T> = string | FilterFunction<T> | T | Cheerio<T>;
Supported filter types, for traversal methods.
type BasicAcceptedElems
type BasicAcceptedElems<T extends AnyNode> = ArrayLike<T> | T | string;
Elements that can be passed to manipulation methods.
type FilterFunction
type FilterFunction<T> = (this: T, i: number, el: T) => boolean;
Function signature, for traversal methods.
type SelectorType
type SelectorType = | `${SelectorSpecial}${AlphaNumeric}${string}` | `${AlphaNumeric}${string}`;
Type for identifying selectors. Allows us to "upgrade" queries using selectors to return
Element
s.
Package Files (7)
Dependencies (11)
Dev Dependencies (26)
- @imgix/js-core
- @octokit/graphql
- @types/jsdom
- @types/node
- @types/whatwg-mimetype
- @typescript-eslint/eslint-plugin
- @typescript-eslint/parser
- @vitest/coverage-v8
- eslint
- eslint-config-prettier
- eslint-plugin-expect-type
- eslint-plugin-jsdoc
- eslint-plugin-n
- eslint-plugin-unicorn
- eslint-plugin-vitest
- husky
- jquery
- jsdom
- lint-staged
- prettier
- prettier-plugin-jsdoc
- tinybench
- tshy
- tsx
- typescript
- vitest
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/cheerio
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/cheerio)
- HTML<a href="https://www.jsdocs.io/package/cheerio"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 5799 ms. - Missing or incorrect documentation? Open an issue for this package.