cheerio

  • Version 1.0.0-rc.12
  • Published
  • 558 kB
  • 7 dependencies
  • MIT license

Install

npm i cheerio
yarn add cheerio
pnpm add cheerio

Overview

Tiny, fast, and elegant implementation of core jQuery designed specifically for the server

Index

Variables

variable _default

const _default: CheerioAPI;
  • The default cheerio instance.

    Deprecated

    Use the function returned by load instead.

variable contains

const contains: (container: AnyNode, contained: AnyNode) => boolean;
  • In order to promote consistency with the jQuery library, users are encouraged to instead use the static method of the same name.

    Returns

    {boolean}

    Example 1

    const $ = cheerio.load('<div><p></p></div>');
    $.contains($('div').get(0), $('p').get(0));
    //=> true
    $.contains($('p').get(0), $('div').get(0));
    //=> false

    Deprecated

variable merge

const merge: <T>(arr1: WritableArrayLike<T>, arr2: ArrayLike<T>) => ArrayLike<T>;
  • In order to promote consistency with the jQuery library, users are encouraged to instead use the static method of the same name.

    Example 1

    const $ = cheerio.load('');
    $.merge([1, 2], [3, 4]);
    //=> [1, 2, 3, 4]

    Deprecated

variable parseHTML

const parseHTML: {
(
this: CheerioAPI,
data: string,
context?: unknown,
keepScripts?: boolean
): AnyNode[];
(this: CheerioAPI, data?: ''): null;
};
  • 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.

    Example 1

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

    Deprecated

    See .

variable root

const root: (this: CheerioAPI) => Cheerio<Document>;
  • Users seeking to access the top-level element of a parsed document should instead use the root static method of a "loaded" Cheerio function.

    Example 1

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

    Deprecated

Functions

function html

html: {
(this: CheerioAPI, options?: CheerioOptions): string;
(this: CheerioAPI, dom?: any, options?: CheerioOptions): string;
};
  • Renders the document.

    Parameter options

    Options for the renderer.

    Returns

    The rendered document.

  • Renders the document.

    Parameter dom

    Element to render.

    Parameter options

    Options for the renderer.

    Returns

    The rendered document.

function load

load: (
content:
| string
| import('domhandler').AnyNode
| import('domhandler').AnyNode[]
| Buffer,
options?: import('./options.js').CheerioOptions | null | undefined,
isDocument?: boolean
) => import('./load.js').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.

    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

function text

text: (this: CheerioAPI | void, elements?: ArrayLike<AnyNode>) => string;
  • Render the document as text.

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

    Parameter elements

    Elements to render.

    Returns

    The rendered document.

function xml

xml: (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode>) => string;
  • Render the document as XML.

    Parameter dom

    Element to render.

    Returns

    THe rendered document.

Classes

class Cheerio

abstract class Cheerio<T> implements ArrayLike<T> {}

    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 AttributesType,
          TraversingType,
          ManipulationType,
          CssType,
          FormsType,
          Iterable<T> {}

            property cheerio

            cheerio: '[cheerio object]';

              property splice

              splice: typeof Array.prototype.slice;

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

                  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 the context scope which searches within the root 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

                    $('.apple', '#fruits').text();
                    //=> Apple
                    $('ul .pear').attr('class');
                    //=> pear
                    $('li[class=orange]').html();
                    //=> Orange

                  interface CheerioOptions

                  interface CheerioOptions extends HTMLParser2Options, Parse5Options {}
                  • 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 for the href and src 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.

                  interface HTMLParser2Options

                  interface HTMLParser2Options extends DomHandlerOptions, ParserOptions {}
                  • Options accepted by htmlparser2, the default parser for XML.

                    See Also

                    • https://github.com/fb55/htmlparser2/wiki/Parser-options

                  interface Parse5Options

                  interface Parse5Options {}
                  • Options for parse5, the default parser for HTML.

                  property scriptingEnabled

                  scriptingEnabled?: boolean;
                  • Disable scripting in parse5, so noscript tags would be parsed.

                  property sourceCodeLocationInfo

                  sourceCodeLocationInfo?: boolean;
                  • Enable location support for parse5.

                  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> = Cheerio<T> | 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 Elements.

                  Package Files (6)

                  Dependencies (7)

                  Dev Dependencies (24)

                  Peer Dependencies (0)

                  No peer dependencies.

                  Badge

                  To add a badge like this onejsDocs.io badgeto 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>