Skip to main content

cheerio

Classes

Interfaces

Deprecated Functions

Loading Functions

Static Functions

Type Aliases

AcceptedElems

Ƭ AcceptedElems<T>: BasicAcceptedElems<T> | (this: T, i: number, el: T) => BasicAcceptedElems<T>

Elements that can be passed to manipulation methods, including functions.

Type parameters

NameType
Textends AnyNode

Defined in

src/types.ts:49


AcceptedFilters

Ƭ AcceptedFilters<T>: string | FilterFunction<T> | T | Cheerio<T>

Supported filter types, for traversal methods.

Type parameters

Name
T

Defined in

src/types.ts:56


AnyNode

Ƭ AnyNode: ParentNode | ChildNode

Defined in

node_modules/domhandler/lib/node.d.ts:22


BasicAcceptedElems

Ƭ BasicAcceptedElems<T>: ArrayLike<T> | T | string

Elements that can be passed to manipulation methods.

Type parameters

NameType
Textends AnyNode

Defined in

src/types.ts:47


FilterFunction

Ƭ FilterFunction<T>: (this: T, i: number, el: T) => boolean

Type parameters

Name
T

Type declaration

▸ (this, i, el): boolean

Function signature, for traversal methods.

Parameters
NameType
thisT
inumber
elT
Returns

boolean

Defined in

src/types.ts:54


ParentNode

Ƭ ParentNode: Document | Element | CDATA

Defined in

node_modules/domhandler/lib/node.d.ts:20


SelectorType

Ƭ SelectorType: `${SelectorSpecial}${AlphaNumeric}${string}` | `${AlphaNumeric}${string}`

Type for identifying selectors. Allows us to "upgrade" queries using selectors to return Elements.

Defined in

src/types.ts:39

Deprecated Functions

html

html(dom, options?): string

Renders the document.

Deprecated

Use html on the loaded instance instead.

Parameters

NameTypeDescription
domBasicAcceptedElems<AnyNode>Element to render.
options?CheerioOptionsOptions for the renderer.

Returns

string

The rendered document.

Defined in

src/index.ts:88


parseHTML

parseHTML(...args): null

The .parseHTML method exported by the Cheerio module is deprecated.

In order to promote consistency with the jQuery library, users are encouraged to instead use the static method of the same name as it is defined on the "loaded" Cheerio factory function.

Deprecated

Use parseHTML on the loaded instance instead.

Example

const $ = cheerio.load('');
$.parseHTML('<b>markup</b>');

Parameters

NameType
...args[data?: null | ""]

Returns

null

Defined in

website/node_modules/typescript/lib/lib.es5.d.ts:329


root

root(...args): Cheerio<Document>

The .root method exported by the Cheerio module is deprecated.

Users seeking to access the top-level element of a parsed document should instead use the root static method of a "loaded" Cheerio function.

Deprecated

Use root on the loaded instance instead.

Example

const $ = cheerio.load('');
$.root();

Parameters

NameType
...args[]

Returns

Cheerio<Document>

Defined in

website/node_modules/typescript/lib/lib.es5.d.ts:329


text

text(elements): string

Render the document as text.

This returns the textContent of the passed elements. The result will include the contents of <script> and <style> elements. To avoid this, use .prop('innerText') instead.

Deprecated

Use text on the loaded instance instead.

Parameters

NameTypeDescription
elementsArrayLike<AnyNode>Elements to render.

Returns

string

The rendered document.

Defined in

src/index.ts:116


xml

xml(dom): string

Render the document as XML.

Deprecated

Use xml on the loaded instance instead.

Parameters

NameTypeDescription
domBasicAcceptedElems<AnyNode>Element to render.

Returns

string

The rendered document.

Defined in

src/index.ts:101


Loading Functions

decodeStream

decodeStream(options, cb): 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.

Parameters

NameTypeDescription
optionsDecodeStreamOptionsThe options to pass to Cheerio.
cb(err: undefined | null | Error, $: CheerioAPI) => voidThe callback to call when the stream is finished.

Returns

Writable

The writable stream.

Defined in

src/batteries.ts:151


fromURL

fromURL(url, options?): Promise<CheerioAPI>

fromURL loads a document from a URL.

By default, redirects are allowed and non-2xx responses are rejected.

Example

import * as cheerio from 'cheerio';

const $ = await cheerio.fromURL('https://example.com');

Parameters

NameTypeDescription
urlstring | URLThe URL to load the document from.
optionsCheerioRequestOptionsThe options to pass to Cheerio.

Returns

Promise<CheerioAPI>

The loaded document.

Defined in

src/batteries.ts:206


load

load(content, options?, isDocument?): 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; set isDocument to false to switch to fragment mode and disable this.

See

https://cheerio.js.org#loading for additional usage information.

Parameters

NameTypeDefault valueDescription
contentstring | AnyNode | AnyNode[] | BufferundefinedMarkup to be loaded.
options?null | CheerioOptionsundefinedOptions for the created instance.
isDocumentbooleantrueAllows parser to be switched to fragment mode.

Returns

CheerioAPI

The loaded document.

Defined in

src/load.ts:126


loadBuffer

loadBuffer(buffer, options?): CheerioAPI

Sniffs the encoding of a buffer, then creates a querying function bound to a document created from the buffer.

Example

import * as cheerio from 'cheerio';

const buffer = fs.readFileSync('index.html');
const $ = cheerio.fromBuffer(buffer);

Parameters

NameTypeDescription
bufferBufferThe buffer to sniff the encoding of.
optionsDecodeStreamOptionsThe options to pass to Cheerio.

Returns

CheerioAPI

The loaded document.

Defined in

src/batteries.ts:44


stringStream

stringStream(options, cb): 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.

Example

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
);

Parameters

NameTypeDescription
optionsCheerioOptionsThe options to pass to Cheerio.
cb(err: undefined | null | Error, $: CheerioAPI) => voidThe callback to call when the stream is finished.

Returns

Writable

The writable stream.

Defined in

src/batteries.ts:129


Static Functions

contains

contains(container, contained): boolean

Checks to see if the contained DOM element is a descendant of the container DOM element.

Alias

Cheerio.contains

See

https://api.jquery.com/jQuery.contains/

Parameters

NameTypeDescription
containerAnyNodePotential parent node.
containedAnyNodePotential child node.

Returns

boolean

Indicates if the nodes contain one another.

Defined in

src/static.ts:216


merge

merge<T>(arr1, arr2): ArrayLike<T> | undefined

$.merge().

Alias

Cheerio.merge

See

https://api.jquery.com/jQuery.merge/

Type parameters

Name
T

Parameters

NameTypeDescription
arr1Writable<ArrayLike<T>>First array.
arr2ArrayLike<T>Second array.

Returns

ArrayLike<T> | undefined

arr1, with elements of arr2 inserted.

Defined in

src/static.ts:265