Skip to main content

Interface: CheerioAPI()

Defined in: src/load.ts:21

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.

Extends​

  • StaticType

CheerioAPI<T, S>(selector?, context?, root?, options?): Cheerio<S extends SelectorType ? Element : T>

Defined in: src/load.ts:67

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.

Type Parameters​

T​

T extends AnyNode

S​

S extends string

Parameters​

selector?​

Either a selector to look for within the document, or the contents of a new Cheerio instance.

S | BasicAcceptedElems<T>

context?​

Either a selector to look for within the root, or the contents of the document to query.

null | BasicAcceptedElems<AnyNode>

root?​

BasicAcceptedElems<Document>

Optional HTML document string.

options?​

CheerioOptions

Returns​

Cheerio<S extends SelectorType ? Element : T>

Example​

$('ul .pear').attr('class');
//=> pear

$('li[class=orange]').html();
//=> Orange

$('.apple', '#fruits').text();
//=> Apple

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

Deprecated​

load()​

load: (content, options?, isDocument) => CheerioAPI

Defined in: src/load.ts:104

The .load static method defined on the "loaded" Cheerio factory function is deprecated. Users are encouraged to instead use the load function exported by the Cheerio module.

Parameters​

content​

string | AnyNode | AnyNode[] | Buffer<ArrayBufferLike>

options?​

null | CheerioOptions

isDocument?​

boolean = true

Returns​

CheerioAPI

Deprecated​

Use the load function exported by the Cheerio module.

Example​

const $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');

Other​

fn​

fn: Cheerio<any>

Defined in: src/load.ts:89

Mimic jQuery's prototype alias for plugin authors.


contains​

Re-exports contains


merge​

Re-exports merge

Static​

extract()​

extract<M>(this, map): ExtractedMap<M>

Defined in: src/static.ts:248

Extract multiple values from a document, and store them in an object.

Type Parameters​

M​

M extends ExtractMap

Parameters​

this​

CheerioAPI

map​

M

An object containing key-value pairs. The keys are the names of the properties to be created on the object, and the values are the selectors to be used to extract the values.

Returns​

ExtractedMap<M>

An object containing the extracted values.

Inherited from​

StaticType.extract


html()​

Call Signature​

html(this, options?): string

Defined in: src/static.ts:58

Renders the document.

Parameters​
this​

CheerioAPI

options?​

CheerioOptions

Options for the renderer.

Returns​

string

The rendered document.

Inherited from​

StaticType.html

Call Signature​

html(this, dom?, options?): string

Defined in: src/static.ts:67

Renders the document.

Parameters​
this​

CheerioAPI

dom?​

BasicAcceptedElems<AnyNode>

Element to render.

options?​

CheerioOptions

Options for the renderer.

Returns​

string

The rendered document.

Inherited from​

StaticType.html


parseHTML()​

Call Signature​

parseHTML(this, data, context?, keepScripts?): AnyNode[]

Defined in: src/static.ts:152

Parses a string into an array of DOM nodes. The context argument has no meaning for Cheerio, but it is maintained for API compatibility with jQuery.

Parameters​
this​

CheerioAPI

data​

string

Markup that will be parsed.

context?​

unknown

Will be ignored. If it is a boolean it will be used as the value of keepScripts.

keepScripts?​

boolean

If false all scripts will be removed.

Returns​

AnyNode[]

The parsed DOM.

Alias​

Cheerio.parseHTML

See​

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

Inherited from​

StaticType.parseHTML

Call Signature​

parseHTML(this, data?): null

Defined in: src/static.ts:158

Parses a string into an array of DOM nodes. The context argument has no meaning for Cheerio, but it is maintained for API compatibility with jQuery.

Parameters​
this​

CheerioAPI

data?​

Markup that will be parsed.

null | ""

Returns​

null

The parsed DOM.

Alias​

Cheerio.parseHTML

See​

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

Inherited from​

StaticType.parseHTML


root()​

root(this): Cheerio<Document>

Defined in: src/static.ts:203

Sometimes you need to work with the top-level root element. To query it, you can use $.root().

Parameters​

this​

CheerioAPI

Returns​

Cheerio<Document>

Cheerio instance wrapping the root node.

Example​

$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>

Alias​

Cheerio.root

Inherited from​

StaticType.root


text()​

text(this, elements?): string

Defined in: src/static.ts:124

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​

this​

void | CheerioAPI

elements?​

ArrayLike<AnyNode>

Elements to render.

Returns​

string

The rendered document.

Inherited from​

StaticType.text


xml()​

xml(this, dom?): string

Defined in: src/static.ts:104

Render the document as XML.

Parameters​

this​

CheerioAPI

dom?​

BasicAcceptedElems<AnyNode>

Element to render.

Returns​

string

THe rendered document.

Inherited from​

StaticType.xml