cheerio vs htmlparser2 vs jsdom vs parse5
HTMLパーサーライブラリ
cheeriohtmlparser2jsdomparse5類似パッケージ:

HTMLパーサーライブラリ

HTMLパーサーライブラリは、HTML文書を解析し、操作するためのツールです。これらのライブラリは、DOM(Document Object Model)を生成し、HTMLコンテンツを操作するための便利なAPIを提供します。これにより、ウェブスクレイピング、データ抽出、HTMLの操作が容易になります。

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

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
cheerio030,3611.01 MB434ヶ月前MIT
htmlparser204,764235 kB163ヶ月前MIT
jsdom021,5817.03 MB4161ヶ月前MIT
parse503,899337 kB362ヶ月前MIT

機能比較: cheerio vs htmlparser2 vs jsdom vs parse5

パフォーマンス

  • cheerio:

    Cheerioは、jQueryのようなAPIを持ちながらも、非常に軽量で高速です。サーバーサイドでのデータ抽出やDOM操作において、パフォーマンスが求められるシナリオに最適です。

  • htmlparser2:

    htmlparser2は、ストリーミングパースをサポートしており、大きなHTML文書を効率的に処理できます。これにより、メモリ使用量を最小限に抑えつつ、高速な解析が可能です。

  • jsdom:

    jsdomは、ブラウザのような環境を提供するため、DOM操作が必要な場合にパフォーマンスが高いです。ただし、他のライブラリに比べてオーバーヘッドが大きくなることがあります。

  • parse5:

    parse5は、HTML5仕様に準拠しており、正確なパースを実現します。パフォーマンスは良好ですが、特にHTML5の構文解析が必要な場合に最適です。

APIの使いやすさ

  • cheerio:

    Cheerioは、jQueryに似たシンプルで直感的なAPIを提供します。これにより、HTMLの操作が容易で、特にスクレイピングにおいて使いやすいです。

  • htmlparser2:

    htmlparser2は、柔軟なAPIを持ちますが、他のライブラリに比べるとやや複雑です。ストリーミングパースを利用する場合は、APIの理解が必要です。

  • jsdom:

    jsdomは、ブラウザのAPIを模倣しているため、DOM操作に慣れている開発者にとって使いやすいです。ただし、全機能を利用するには、ブラウザの知識が必要です。

  • parse5:

    parse5は、HTML5仕様に基づいたAPIを提供しますが、他のライブラリに比べるとやや難解です。特に、HTML5の詳細な理解が求められます。

エコシステムとサポート

  • cheerio:

    Cheerioは、jQueryのエコシステムを活用できるため、豊富なプラグインやリソースが利用可能です。コミュニティも活発で、サポートも充実しています。

  • htmlparser2:

    htmlparser2は、Node.jsのエコシステムに深く統合されており、他のライブラリとの互換性が高いです。ドキュメントも充実しています。

  • jsdom:

    jsdomは、テストフレームワークとの統合が容易で、特にJestやMochaなどのユニットテストに適しています。サポートも充実しています。

  • parse5:

    parse5は、HTML5の仕様に準拠しているため、正確なパースが求められるプロジェクトに最適です。ただし、他のライブラリに比べるとエコシステムは小さいです。

柔軟性と拡張性

  • cheerio:

    Cheerioは、jQueryスタイルのAPIを持つため、DOM操作において非常に柔軟です。また、簡単に拡張可能で、カスタムプラグインを作成することもできます。

  • htmlparser2:

    htmlparser2は、ストリーミングパースをサポートしており、非常に柔軟です。特に、大規模なHTML文書を扱う場合に適しています。

  • jsdom:

    jsdomは、ブラウザのAPIを模倣しているため、DOM操作やイベントハンドリングの柔軟性があります。特に、テスト環境での利用において強力です。

  • parse5:

    parse5は、HTML5仕様に準拠しているため、正確なパースが可能ですが、拡張性は他のライブラリに比べて制限されることがあります。

学習曲線

  • cheerio:

    Cheerioは、jQueryに似たAPIを持つため、jQueryに慣れている開発者にとっては学習が容易です。特に、スクレイピング初心者にも適しています。

  • htmlparser2:

    htmlparser2は、ストリーミングパースの概念を理解する必要があるため、やや学習曲線が急です。しかし、柔軟性が高い分、習得すれば非常に強力です。

  • jsdom:

    jsdomは、ブラウザのAPIを模倣しているため、DOM操作に慣れている開発者にとっては比較的学習しやすいですが、全機能を理解するには時間がかかることがあります。

  • parse5:

    parse5は、HTML5仕様に基づいているため、特にHTML5の知識が必要です。学習曲線はやや急ですが、正確なパースが求められる場合には価値があります。

選び方: cheerio vs htmlparser2 vs jsdom vs parse5

  • cheerio:

    Cheerioは、jQueryに似たAPIを提供し、軽量で高速なHTML操作が必要な場合に最適です。特に、サーバーサイドでのスクレイピングやデータ抽出に向いています。

  • htmlparser2:

    htmlparser2は、非常に柔軟で、高速なHTMLパーサーが必要な場合に選択するべきです。ストリーミングパースが可能で、大きなHTML文書を効率的に処理できます。

  • jsdom:

    jsdomは、ブラウザ環境をエミュレートするため、DOM操作やイベントハンドリングが必要な場合に最適です。テスト環境や、クライアントサイドの機能をサーバーサイドで模倣する際に便利です。

  • parse5:

    parse5は、HTML5仕様に準拠したパーサーが必要な場合に選ぶべきです。特に、HTML5の構文解析を正確に行いたい場合に適しています。

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