Skip to main content
Version: Next

externalCheerioAPI

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

  • CheerioAPI<T, S>(selector, context, root, options): 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.


    Parameters

    • externaloptionalselector: S | BasicAcceptedElems<T>

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

    • externaloptionalcontext: null | BasicAcceptedElems<AnyNode>

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

    • externaloptionalroot: BasicAcceptedElems<Document>

      Optional HTML document string.

    • externaloptionaloptions: CheerioOptions

    Returns Cheerio<S extends SelectorType ? Element : T>

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

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

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

Index

Properties

externalfn

fn: Cheerio<any>

Mimic jQuery's prototype alias for plugin authors.

externalload

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

Type declaration

    • Parameters

      • externalcontent: string | Buffer<ArrayBufferLike> | AnyNode | AnyNode[]
      • externaloptionaloptions: null | CheerioOptions
      • externaloptionalisDocument: boolean

      Returns CheerioAPI

Methods

externalinheritedcontains

  • contains(container, contained): boolean
  • Checks to see if the contained DOM element is a descendant of the container DOM element.


    Parameters

    • externalcontainer: AnyNode

      Potential parent node.

    • externalcontained: AnyNode

      Potential child node.

    Returns boolean

    Indicates if the nodes contain one another.

    Cheerio.contains

externalinheritedhtml

  • html(this, options): string
  • html(this, dom, options): string
  • Renders the document.


    Parameters

    • externalthis: CheerioAPI
    • externaloptionaloptions: CheerioOptions

      Options for the renderer.

    Returns string

    The rendered document.

externalinheritedmerge

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


    Parameters

    • externalarr1: WritableArrayLike<T>

      First array.

    • externalarr2: ArrayLike<T>

      Second array.

    Returns ArrayLike<T> | undefined

    arr1, with elements of arr2 inserted.

    Cheerio.merge

externalinheritedparseHTML

  • parseHTML(this, data, context, keepScripts): AnyNode[]
  • parseHTML(this, data): 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.


    Parameters

    • externalthis: CheerioAPI
    • externaldata: string

      Markup that will be parsed.

    • externaloptionalcontext: unknown

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

    • externaloptionalkeepScripts: boolean

      If false all scripts will be removed.

    Returns AnyNode[]

    The parsed DOM.

    Cheerio.parseHTML

externalinheritedroot

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


    Parameters

    Returns Cheerio<Document>

    Cheerio instance wrapping the root node.

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

    Cheerio.root

externalinheritedtext

  • text(this, elements): 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.


    Parameters

    • externalthis: void | CheerioAPI
    • externaloptionalelements: ArrayLike<AnyNode>

      Elements to render.

    Returns string

    The rendered document.

externalinheritedxml

  • xml(this, dom): string
  • Render the document as XML.


    Parameters

    • externalthis: CheerioAPI
    • externaloptionaldom: BasicAcceptedElems<AnyNode>

      Element to render.

    Returns string

    THe rendered document.