Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface CheerioAPI

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.

Hierarchy

  • StaticType
    • CheerioAPI

Callable

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

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

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

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

    Type parameters

    Parameters

    Returns Cheerio<S extends SelectorType ? Element : T>

Index

Properties

Private _options

_options: InternalOptions

The options the document was originally loaded with.

Private _root

_root: Document

The root the document was originally loaded with.

fn

fn: Cheerio<any>

Mimic jQuery's prototype alias for plugin authors.

load

load: (content: string | Node | Node[] | Buffer, options?: null | CheerioOptions, isDocument?: boolean) => CheerioAPI

Type declaration

    • 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

      • content: string | Node | Node[] | Buffer

        Markup to be loaded.

      • Optional options: null | CheerioOptions

        Options for the created instance.

      • isDocument: boolean = true

        Allows parser to be switched to fragment mode.

      Returns CheerioAPI

      The loaded document.

Methods

contains

  • contains(container: Node, contained: Node): 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

    • container: Node

      Potential parent node.

    • contained: Node

      Potential child node.

    Returns boolean

    Indicates if the nodes contain one another.

html

  • Renders the document.

    Parameters

    Returns string

    The rendered document.

  • Renders the document.

    Parameters

    Returns string

    The rendered document.

merge

  • merge<T>(arr1: WritableArrayLike<T>, arr2: ArrayLike<T>): ArrayLike<T> | undefined
  • $.merge().

    alias

    Cheerio.merge

    see

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

    Type parameters

    • T

    Parameters

    • arr1: WritableArrayLike<T>

      First array.

    • arr2: ArrayLike<T>

      Second array.

    Returns ArrayLike<T> | undefined

    arr1, with elements of arr2 inserted.

parseHTML

  • parseHTML(data: string, context?: unknown, keepScripts?: boolean): Node[]
  • parseHTML(data?: null | ""): null
  • 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.

    alias

    Cheerio.parseHTML

    see

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

    Parameters

    • data: string

      Markup that will be parsed.

    • Optional context: unknown

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

    • Optional keepScripts: boolean

      If false all scripts will be removed.

    Returns Node[]

    The parsed DOM.

  • Parameters

    • Optional data: null | ""

    Returns null

root

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

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

    Cheerio.root

    Returns Cheerio<Document>

    Cheerio instance wrapping the root node.

text

  • text(elements?: ArrayLike<Node>): string
  • Render the document as text.

    Parameters

    • Optional elements: ArrayLike<Node>

      Elements to render.

    Returns string

    The rendered document.

xml