cheerio vs domino vs jsdom vs puppeteer
WebスクレイピングとDOM操作ライブラリ
cheeriodominojsdompuppeteer類似パッケージ:

WebスクレイピングとDOM操作ライブラリ

これらのライブラリは、Node.js環境でのウェブスクレイピングやDOM操作を容易にするために設計されています。これらのパッケージは、HTMLを解析し、DOMを操作するためのAPIを提供し、ウェブページからデータを抽出したり、操作したりするのに役立ちます。特に、サーバーサイドでの処理やテスト自動化において非常に有用です。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
cheerio030,3441.01 MB454ヶ月前MIT
domino0787827 kB302ヶ月前BSD-2-Clause
jsdom021,5777.03 MB4141ヶ月前MIT
puppeteer094,37240.2 kB26212時間前Apache-2.0

機能比較: cheerio vs domino vs jsdom vs puppeteer

パフォーマンス

  • cheerio:

    Cheerioは、非常に軽量で高速なHTML解析を提供します。DOMの操作が迅速で、サーバーサイドでのデータ抽出に適しています。

  • domino:

    Dominoは、シンプルなDOM実装を提供しますが、機能が限られているため、パフォーマンスは他のライブラリに比べて劣ります。

  • jsdom:

    jsdomは、完全なDOMをエミュレートするため、パフォーマンスは比較的高いですが、実際のブラウザ環境と同等の動作をするため、リソースを多く消費します。

  • puppeteer:

    Puppeteerは、ヘッドレスブラウザを使用するため、ページのレンダリングに時間がかかることがありますが、動的なコンテンツを扱う際には非常に強力です。

APIの使いやすさ

  • cheerio:

    Cheerioは、jQueryに似たAPIを提供しており、DOM操作が直感的で簡単です。

  • domino:

    Dominoは、シンプルなAPIを持っていますが、機能が限られているため、複雑な操作には不向きです。

  • jsdom:

    jsdomは、ブラウザのAPIに近い形でDOMを操作できるため、使いやすさは高いですが、学習コストがかかる場合があります。

  • puppeteer:

    Puppeteerは、豊富なAPIを提供しており、ブラウザ操作が強力ですが、やや複雑です。

機能の豊富さ

  • cheerio:

    Cheerioは、基本的なDOM操作に特化しており、機能はシンプルですが、必要な機能は揃っています。

  • domino:

    Dominoは、基本的なDOM機能のみを提供しており、機能は非常に限られています。

  • jsdom:

    jsdomは、HTML5の完全な実装を提供し、テストやクライアントサイドのJavaScriptの実行において非常に多機能です。

  • puppeteer:

    Puppeteerは、ページのレンダリング、スクリーンショット、PDF生成など、非常に多くの機能を提供しています。

ユースケース

  • cheerio:

    Cheerioは、サーバーサイドでのデータ抽出やHTMLの解析に最適です。

  • domino:

    Dominoは、シンプルなDOM操作が必要な場合に適していますが、複雑なユースケースには不向きです。

  • jsdom:

    jsdomは、テスト環境やクライアントサイドのJavaScriptをサーバーサイドで実行する必要がある場合に最適です。

  • puppeteer:

    Puppeteerは、動的なコンテンツを扱う必要がある場合や、ユーザーインタラクションをシミュレートしたい場合に最適です。

学習曲線

  • cheerio:

    Cheerioは、jQueryに似たAPIを持っているため、学習曲線は緩やかです。

  • domino:

    Dominoは、シンプルなAPIを持っているため、学習は容易ですが、機能が限られています。

  • jsdom:

    jsdomは、ブラウザのAPIに近いため、学習には少し時間がかかるかもしれませんが、使いこなせれば強力です。

  • puppeteer:

    Puppeteerは、多機能であるため、学習曲線は急ですが、使いこなすことで強力なツールとなります。

選び方: cheerio vs domino vs jsdom vs puppeteer

  • cheerio:

    Cheerioは、軽量で高速なHTML解析ライブラリで、jQueryのようなAPIを提供します。DOM操作が必要な場合や、サーバーサイドでのデータ抽出を行う場合に最適です。

  • domino:

    Dominoは、軽量でシンプルなDOM実装を提供します。主に、Node.js環境でのDOM操作を行いたい場合に選択されますが、機能は制限されているため、複雑な操作には向いていません。

  • jsdom:

    jsdomは、完全なDOMおよびHTML5の実装を提供し、ブラウザ環境に近い動作を実現します。テスト環境や、クライアントサイドのJavaScriptをサーバーサイドで実行する必要がある場合に適しています。

  • puppeteer:

    Puppeteerは、ヘッドレスChromeブラウザを操作するためのライブラリで、ページのレンダリングやスクリーンショット、PDF生成などが可能です。動的なコンテンツを扱う必要がある場合や、ユーザーインタラクションをシミュレートしたい場合に最適です。

cheerio のREADME

cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.

中文文档 (Chinese Readme)

import * as cheerio from 'cheerio';
const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$('h2.title').text('Hello there!');
$('h2').addClass('welcome');

$.html();
//=> <html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>

Installation

Install Cheerio using a package manager like npm, yarn, or bun.

npm install cheerio
# or
bun add cheerio

Features

❤ Proven syntax: Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing its truly gorgeous API.

ϟ Blazingly fast: Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient.

❁ Incredibly flexible: Cheerio wraps around parse5 for parsing HTML and can optionally use the forgiving htmlparser2. Cheerio can parse nearly any HTML or XML document. Cheerio works in both browser and server environments.

API

Loading

First you need to load in the HTML. This step in jQuery is implicit, since jQuery operates on the one, baked-in DOM. With Cheerio, we need to pass in the HTML document.

// ESM or TypeScript:
import * as cheerio from 'cheerio';

// In other environments:
const cheerio = require('cheerio');

const $ = cheerio.load('<ul id="fruits">...</ul>');

$.html();
//=> <html><head></head><body><ul id="fruits">...</ul></body></html>

Selectors

Once you've loaded the HTML, you can use jQuery-style selectors to find elements within the document.

$( selector, [context], [root] )

selector searches within the context scope which searches within the root scope. selector and context can be a string expression, DOM Element, array of DOM elements, or cheerio object. root, if provided, is typically the HTML document string.

This selector method is the starting point for traversing and manipulating the document. Like in jQuery, it's the primary method for selecting elements in the document.

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

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

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

Rendering

When you're ready to render the document, you can call the html method on the "root" selection:

$.root().html();
//=>  <html>
//      <head></head>
//      <body>
//        <ul id="fruits">
//          <li class="apple">Apple</li>
//          <li class="orange">Orange</li>
//          <li class="pear">Pear</li>
//        </ul>
//      </body>
//    </html>

If you want to render the outerHTML of a selection, you can use the outerHTML prop:

$('.pear').prop('outerHTML');
//=> <li class="pear">Pear</li>

You may also render the text content of a Cheerio object using the text method:

const $ = cheerio.load('This is <em>content</em>.');
$('body').text();
//=> This is content.

The "DOM Node" object

Cheerio collections are made up of objects that bear some resemblance to browser-based DOM nodes. You can expect them to define the following properties:

  • tagName
  • parentNode
  • previousSibling
  • nextSibling
  • nodeValue
  • firstChild
  • childNodes
  • lastChild

Screencasts

https://vimeo.com/31950192

This video tutorial is a follow-up to Nettut's "How to Scrape Web Pages with Node.js and jQuery", using cheerio instead of JSDOM + jQuery. This video shows how easy it is to use cheerio and how much faster cheerio is than JSDOM + jQuery.

Cheerio in the real world

Are you using cheerio in production? Add it to the wiki!

Sponsors

Does your company use Cheerio in production? Please consider sponsoring this project! Your help will allow maintainers to dedicate more time and resources to its development and support.

Headlining Sponsors

Tidelift Github AirBnB HasData

Other Sponsors

OnlineCasinosSpelen Nieuwe-Casinos.net

Backers

Become a backer to show your support for Cheerio and help us maintain and improve this open source project.

Vasy Kafidoff

License

MIT