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
Name | Type |
---|---|
T | extends AnyNode |
Defined in
AcceptedFilters
Ƭ AcceptedFilters<T
>: string
| FilterFunction
<T
> | T
| Cheerio
<T
>
Supported filter types, for traversal methods.
Type parameters
Name |
---|
T |
Defined in
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
Name | Type |
---|---|
T | extends AnyNode |
Defined in
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
Name | Type |
---|---|
this | T |
i | number |
el | T |
Returns
boolean
Defined in
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 Element
s.
Defined in
Deprecated Functions
html
▸ html(dom
, options?
): string
Renders the document.
Deprecated
Use html
on the loaded instance instead.
Parameters
Name | Type | Description |
---|---|---|
dom | BasicAcceptedElems <AnyNode > | Element to render. |
options? | CheerioOptions | Options for the renderer. |
Returns
string
The rendered document.
Defined in
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
Name | Type |
---|---|
...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
Name | Type |
---|---|
...args | [] |
Returns
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
Name | Type | Description |
---|---|---|
elements | ArrayLike <AnyNode > | Elements to render. |
Returns
string
The rendered document.
Defined in
xml
▸ xml(dom
): string
Render the document as XML.
Deprecated
Use xml
on the loaded instance instead.
Parameters
Name | Type | Description |
---|---|---|
dom | BasicAcceptedElems <AnyNode > | Element to render. |
Returns
string
The rendered document.
Defined in
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
Name | Type | Description |
---|---|---|
options | DecodeStreamOptions | The options to pass to Cheerio. |
cb | (err : undefined | null | Error , $ : CheerioAPI ) => void | The callback to call when the stream is finished. |
Returns
Writable
The writable stream.
Defined in
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
Name | Type | Description |
---|---|---|
url | string | URL | The URL to load the document from. |
options | CheerioRequestOptions | The options to pass to Cheerio. |
Returns
The loaded document.
Defined in
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
Name | Type | Default value | Description |
---|---|---|---|
content | string | AnyNode | AnyNode [] | Buffer | undefined | Markup to be loaded. |
options? | null | CheerioOptions | undefined | Options for the created instance. |
isDocument | boolean | true | Allows parser to be switched to fragment mode. |
Returns
The loaded document.
Defined in
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
Name | Type | Description |
---|---|---|
buffer | Buffer | The buffer to sniff the encoding of. |
options | DecodeStreamOptions | The options to pass to Cheerio. |
Returns
The loaded document.
Defined in
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
Name | Type | Description |
---|---|---|
options | CheerioOptions | The options to pass to Cheerio. |
cb | (err : undefined | null | Error , $ : CheerioAPI ) => void | The callback to call when the stream is finished. |
Returns
Writable
The writable stream.
Defined in
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
Name | Type | Description |
---|---|---|
container | AnyNode | Potential parent node. |
contained | AnyNode | Potential child node. |
Returns
boolean
Indicates if the nodes contain one another.
Defined in
merge
▸ merge<T
>(arr1
, arr2
): ArrayLike
<T
> | undefined
$.merge().
Alias
Cheerio.merge
See
https://api.jquery.com/jQuery.merge/
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr1 | Writable <ArrayLike <T >> | First array. |
arr2 | ArrayLike <T > | Second array. |
Returns
ArrayLike
<T
> | undefined
arr1
, with elements of arr2
inserted.