cheerio README

The project's README from github.com/cheeriojs/cheerio. As one markdown file: README.md.

cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
Build Status Coverage OpenCollective backers OpenCollective sponsors OpenSSF Scorecard

中文文档 (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, bun, or Deno.

npm install cheerio
# or
bun add cheerio
# or
deno install 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:

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

ScrapingBee Github AirBnB

Other Sponsors

OnlineCasinosSpelen Nieuwe-Casinos.net Rapidproxy

Backers

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

Vasy Kafidoff

License

MIT

cheerio (cheeriojs/cheerio)

Searching Database…

30.5K ·GitHub
这是什么?Cheerio 是一个快速、灵活的解析和操作 HTML 与 XML 的库。它实现了 jQuery 核心 API 的一个子集,所以只要你熟悉 jQuery 选择器,就已经掌握了它的大部分用法。
它基本上就是 jQuery,还是别的东西?它是一个 JavaScript 库,实现了核心 jQuery 的一个子集,但去掉了 DOM 的不一致性和浏览器的各种兼容包袱。和 jQuery 不同的是,它没有内置的页面 DOM —— 你需要自己加载想要处理的 HTML。
怎么把它加到项目里?用任意 JavaScript 包管理器安装 —— npm、yarn、bun 或 Deno 都行(npm install cheerio)。然后把 HTML 字符串加载进来,用 jQuery 风格的选择器查找元素,再用 html() 或 text() 输出结果。
它需要哪个 Node 版本?还有别的东西要配置吗?这个包声明的 Node 版本要求是 >= 22.19.0,相当新 —— 先检查一下你的运行环境。除此之外不需要任何配置:解析引擎都作为内置依赖一起打包了。
它能处理实际中那些乱七八糟的 HTML 和 XML 吗?能 —— 它几乎可以解析任何 HTML 或 XML 文档。默认用 parse5 来解析 HTML,也可以选择更宽容的 htmlparser2,所以再烂的标记也拦不住它。
它只能在 Node 下运行,浏览器里也行吗?都可以 —— README 里说它在浏览器和服务器环境都能用。由于它是纯 TypeScript/JavaScript、不含原生代码,对操作系统没有任何要求。
我选中并修改元素之后,怎么把结果取出来?对根选中集调用 html() 就能渲染整个文档,读取 outerHTML 属性可以得到某个选中集的外层 HTML,或者只想要文本内容时用 text()。
选择器调用有多灵活?主要的 $(selector, context, root) 方法会在 context 范围内搜索,而 context 又在 root 范围内搜索。selector 和 context 可以是字符串表达式、DOM 元素、DOM 元素数组,或者一个 Cheerio 对象。
做网页抓取时,为什么用它而不用 JSDOM 加 jQuery?README 自带的演示视频就展示了 Cheerio 比 JSDOM + jQuery 快多少:它简单一致的 DOM 模型让解析、操作和输出都非常高效。
选中的元素用起来像真正的 DOM 节点吗?它们和浏览器里的 DOM 节点非常相似 —— 你可以在它们身上用到熟悉的属性,比如 tagName、parentNode、previousSibling、nextSibling、firstChild 和 childNodes。
What are similar projects?goquery, marked, jquery, turndown, masonry, jsoup, jsdom, velocity
Creator
cheeriojs
Size
21.6 MB
Stars
30.5K
Age
15y
Commits · 90 Days