DE EN ES FR ID JA KO PT RU TH VI ZH

Cheerio Advanced API Reference

This reference covers advanced methods, utilities, configuration options, and types that extend beyond Cheerio's core functionality.

Advanced Loading Methods

loadBuffer(buffer, options?)

Loads HTML/XML from a Buffer with automatic encoding detection.

loadBuffer(buffer: Buffer, options?: DecodeStreamOptions): CheerioAPI

Parameters:

Example:

import * as cheerio from 'cheerio';
import * as fs from 'fs';

const buffer = fs.readFileSync('index.html');
const $ = cheerio.loadBuffer(buffer);

console.log($('title').text());

Encoding Detection:

const $ = cheerio.loadBuffer(buffer, {
  encoding: {
    defaultEncoding: 'utf8',
    transportLayerEncodingLabel: 'windows-1252'
  }
});

fromURL(url, options?)

Loads HTML/XML directly from a URL with built-in HTTP client.

fromURL(url: string | URL, options?: CheerioRequestOptions): Promise<CheerioAPI>

Parameters:

Example:

const $ = await cheerio.fromURL('https://example.com');
console.log($('h1').text());

// With custom headers and options
const $page = await cheerio.fromURL('https://api.example.com/data', {
  requestOptions: {
    headers: { 'User-Agent': 'MyBot/1.0' },
    method: 'GET'
  },
  xmlMode: false,
  baseURI: 'https://api.example.com'
});

Error Handling:

try {
  const $ = await cheerio.fromURL('https://invalid-url.com');
} catch (error) {
  if (error instanceof undici.errors.ResponseError) {
    console.log(`HTTP ${error.statusCode}: Request failed`);
  }
}

stringStream(options, callback)

Creates a writable stream for parsing HTML/XML chunks.

stringStream(
  options: CheerioOptions,
  cb: (err: Error | null, $: CheerioAPI) => void
): Writable

Example:

import * as fs from 'fs';

const writeStream = cheerio.stringStream({}, (err, $) => {
  if (err) throw err;
  console.log($('title').text());
});

fs.createReadStream('large-file.html', { encoding: 'utf8' })
  .pipe(writeStream);

Data Extraction

.extract(map)

Extract multiple values from elements using declarative mapping.

extract<M extends ExtractMap>(map: M): ExtractedMap<M>

Basic Extraction:

const $ = cheerio.load(`
  <div>
    <h1 class="title">Welcome</h1>
    <p class="content">Hello world</p>
    <a href="/page" class="link">Click here</a>
  </div>
`);

const data = $.extract({
  title: 'h1.title',
  content: '.content',
  link: '.link'
});
// Result: { title: 'Welcome', content: 'Hello world', link: 'Click here' }

Array Extraction:

const $ = cheerio.load(`
  <ul>
    <li class="item">Item 1</li>
    <li class="item">Item 2</li>
    <li class="item">Item 3</li>
  </ul>
`);

const data = $.extract({
  items: ['.item'] // Array syntax for multiple elements
});
// Result: { items: ['Item 1', 'Item 2', 'Item 3'] }

Advanced Extraction with Custom Properties:

const data = $.extract({
  links: [{
    selector: 'a',
    value: 'href' // Extract href attribute instead of text
  }],
  metadata: {
    selector: 'head',
    value: {
      title: 'title',
      description: 'meta[name="description"]'
    }
  }
});

Custom Extraction Functions:

const data = $.extract({
  wordCount: {
    selector: 'p',
    value: (el, key) => $(el).text().split(' ').length
  },
  processedContent: {
    selector: '.content',
    value: (el) => $(el).text().toUpperCase().trim()
  }
});

Form Handling

.serialize()

Encode form elements as URL-encoded string.

serialize(): string

Example:

const $ = cheerio.load(`
  <form>
    <input name="username" value="john_doe" />
    <input name="email" value="john@example.com" />
    <select name="country">
      <option value="us" selected>United States</option>
      <option value="ca">Canada</option>
    </select>
  </form>
`);

const serialized = $('form').serialize();
// Result: "username=john_doe&email=john%40example.com&country=us"

.serializeArray()

Encode form elements as an array of name-value pairs.

serializeArray(): Array<{ name: string; value: string }>

Example:

const data = $('form').serializeArray();
// Result: [
//   { name: 'username', value: 'john_doe' },
//   { name: 'email', value: 'john@example.com' },
//   { name: 'country', value: 'us' }
// ]

Complex Forms:

const $ = cheerio.load(`
  <form>
    <input name="tags" value="javascript" type="checkbox" checked />
    <input name="tags" value="nodejs" type="checkbox" checked />
    <textarea name="comment">Great article!</textarea>
  </form>
`);

const formData = $('form').serializeArray();
// Handles multiple values, textareas, and checkboxes automatically

Advanced CSS Methods

.css() Advanced Usage

Function-based Setting:

$('div').css('width', function(index, currentValue) {
  return (parseInt(currentValue) || 0) + 10 + 'px';
});

Bulk Operations:

$('p').css({
  'font-size': '14px',
  'line-height': '1.5',
  'margin-bottom': '1em'
});

Array-based Getting:

const styles = $('header').css(['color', 'background-color', 'font-size']);
// Returns object with requested properties

Manipulation Advanced

.wrap() with Functions

Dynamic Wrapping:

$('img').wrap(function(index) {
  const src = $(this).attr('src');
  return `<figure class="image-${index}">`;
});

.wrapAll() and .wrapInner()

Wrap Multiple Elements:

$('.related-posts').wrapAll('<section class="sidebar">');

Wrap Inner Content:

$('blockquote').wrapInner('<div class="quote-content">');

.unwrap() with Selectors

Conditional Unwrapping:

$('span').unwrap('.temporary-wrapper'); // Only unwrap from .temporary-wrapper
$('em').unwrap(); // Unwrap from any parent

Property and Attribute Advanced

.prop() Special Cases

URL Resolution:

const $ = cheerio.load('<a href="page.html">Link</a>', {
  baseURI: 'https://example.com/blog/'
});

console.log($('a').prop('href')); // 'https://example.com/blog/page.html'

DOM Properties:

const $input = $('<input type="checkbox" checked>');
console.log($input.prop('checked')); // true
console.log($input.attr('checked')); // 'checked'

$input.prop('checked', false);
console.log($input.attr('checked')); // undefined (removed)

.data() Advanced Features

Type Coercion:

const $ = cheerio.load(`
  <div 
    data-count="42"
    data-active="true"
    data-config='{"theme": "dark"}'
    data-list="[1,2,3]"
    data-empty="null"
  ></div>
`);

const data = $('div').data();
// Result: {
//   count: 42,           // number
//   active: true,        // boolean
//   config: {theme: "dark"}, // object
//   list: [1,2,3],       // array
//   empty: null          // null
// }

Camel Case Conversion:

<div data-user-name="john" data-user-id="123"></div>

$('div').data('userName'); // "john"
$('div').data('userId');   // "123"

Configuration Options

CheerioOptions Interface

interface CheerioOptions {
  xmlMode?: boolean;
  decodeEntities?: boolean;
  lowerCaseAttributeNames?: boolean;
  recognizeSelfClosing?: boolean;
  recognizeCDATA?: boolean;
  baseURI?: string;
  _useHtmlParser2?: boolean;
}

XML Mode:

const $ = cheerio.load(xmlString, {
  xmlMode: true,
  recognizeSelfClosing: true
});

HTML Entity Handling:

const $ = cheerio.load(html, {
  decodeEntities: false // Keep entities as-is
});

Base URI for Link Resolution:

const $ = cheerio.load(html, {
  baseURI: 'https://example.com/articles/'
});

$('a').each((i, el) => {
  console.log($(el).prop('href')); // Resolves relative URLs
});

HTMLParser2Options

interface HTMLParser2Options {
  lowerCaseAttributeNames?: boolean;
  recognizeSelfClosing?: boolean;
  recognizeCDATA?: boolean;
  xmlMode?: boolean;
  decodeEntities?: boolean;
}

TypeScript Types

Core Types

import type { 
  CheerioAPI, 
  Cheerio, 
  Element, 
  AnyNode,
  CheerioOptions 
} from 'cheerio';

// Custom element processing
function processElements(elements: Cheerio<Element>) {
  elements.each((index, element) => {
    if (element.tagName === 'img') {
      // Process images
    }
  });
}

Advanced Type Usage

// Type-safe extraction
interface ArticleData {
  title: string;
  author: string;
  publishDate: string;
  tags: string[];
}

function extractArticle($: CheerioAPI): ArticleData {
  return $.extract({
    title: 'h1',
    author: '.author',
    publishDate: '.date',
    tags: ['.tag']
  }) as ArticleData;
}

Utility Functions

Static Methods

import { contains, merge } from 'cheerio';

// Check if one element contains another
const isContained = contains(parentElement, childElement);

// Merge multiple Cheerio objects
const combined = merge($('.class1'), $('.class2'));

Error Handling and Edge Cases

Graceful Degradation

function safeExtract($: CheerioAPI) {
  try {
    return $.extract({
      title: 'h1',
      content: '.content'
    });
  } catch (error) {
    console.warn('Extraction failed:', error);
    return { title: undefined, content: undefined };
  }
}

Empty Collections

const $empty = $('.nonexistent');
console.log($empty.length); // 0
console.log($empty.text());  // ""
console.log($empty.attr('class')); // undefined

// Chaining still works
$empty.addClass('test').removeClass('other'); // No-op, returns $empty

Memory Considerations

// For large documents, consider streaming
const stream = cheerio.stringStream({ xmlMode: true }, (err, $) => {
  if (err) throw err;
  
  // Process document
  const data = $.extract({ /* ... */ });
  
  // Clean up if needed
  $ = null;
});

This advanced API reference covers the extended functionality of Cheerio beyond basic DOM manipulation, providing powerful tools for data extraction, form handling, streaming, and type-safe operations.