Skip to main content

cheerio

Classes

Interfaces

Deprecated

html

html(dom, options?): string

Renders the document.

Parameters

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

Returns

string

The rendered document.

Deprecated

Use html on the loaded instance instead.

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.

Parameters

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

Returns

null

Deprecated

Use parseHTML on the loaded instance instead.

Example

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

Defined in

src/index.ts:135


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.

Parameters

NameType
...args[]

Returns

Cheerio<Document>

Deprecated

Use root on the loaded instance instead.

Example

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

Defined in

src/index.ts:152


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.

Parameters

NameTypeDescription
elementsArrayLike<AnyNode>Elements to render.

Returns

string

The rendered document.

Deprecated

Use text on the loaded instance instead.

Defined in

src/index.ts:116


xml

xml(dom): string

Render the document as XML.

Parameters

NameTypeDescription
domBasicAcceptedElems<AnyNode>Element to render.

Returns

string

The rendered document.

Deprecated

Use xml on the loaded instance instead.

Defined in

src/index.ts:101

Loading

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.

Parameters

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

Returns

Promise<CheerioAPI>

The loaded document.

Example

import * as cheerio from 'cheerio';

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

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.

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.

See

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

Defined in

src/index.ts:55


loadBuffer

loadBuffer(buffer, options?): CheerioAPI

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

Parameters

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

Returns

CheerioAPI

The loaded document.

Example

import * as cheerio from 'cheerio';

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

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.

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.

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

Defined in

src/batteries.ts:129

Other

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

Function signature, for traversal methods.

Type parameters

Name
T

Type declaration

▸ (this, i, el): boolean

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

Static

contains

contains(container, contained): boolean

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

Parameters

NameTypeDescription
containerAnyNodePotential parent node.
containedAnyNodePotential child node.

Returns

boolean

Indicates if the nodes contain one another.

Alias

Cheerio.contains

See

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

Defined in

src/index.ts:77


merge

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

$.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.

Alias

Cheerio.merge

See

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

Defined in

src/index.ts:77