const cheerio = require('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>
We are currently working on the 1.0.0 release of cheerio on the main
branch. The source code for the last published version, 0.22.0
, can be found here.
npm install cheerio
ϟ 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 parser and can optionally use @FB55's forgiving htmlparser2. Cheerio can parse nearly any HTML or XML document.
Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. It does not interpret the result as a web browser does. Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript which is common for a SPA (single page application). This makes Cheerio much, much faster than other solutions. If your use case requires any of this functionality, you should consider browser automation software like Puppeteer and Playwright or DOM emulation projects like JSDom.
<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>
This is the HTML markup we will be using in all of the API examples.
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.
This is the preferred method:
// ES6 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>
Similar to web browser contexts, load
will introduce <html>
, <head>
, and <body>
elements if they are not already present. You can set load
's third argument to false
to disable this.
const $ = cheerio.load('<ul id="fruits">...</ul>', null, false);
$.html();
//=> '<ul id="fruits">...</ul>'
Optionally, you can also load in the HTML by passing the string as the context:
$('ul', '<ul id="fruits">...</ul>');
Or as the root:
$('li', 'ul', '<ul id="fruits">...</ul>');
If you need to modify parsing options for XML input, you may pass an extraobject to .load()
:
const $ = cheerio.load('<ul id="fruits">...</ul>', {
xml: {
normalizeWhitespace: true,
},
});
The options in the xml
object are taken directly from htmlparser2, therefore any options that can be used in htmlparser2
are valid in cheerio as well. When xml
is set, the default options are:
{
xmlMode: true,
decodeEntities: true, // Decode HTML entities.
withStartIndices: false, // Add a `startIndex` property to nodes.
withEndIndices: false, // Add an `endIndex` property to nodes.
}
For a full list of options and their effects, see domhandler andhtmlparser2's options.
htmlparser2
Cheerio ships with two parsers, parse5
and htmlparser2
. Theformer is the default for HTML, the latter the default for XML.
Some users may wish to parse markup with the htmlparser2
library, andtraverse/manipulate the resulting structure with Cheerio. This may be the casefor those upgrading from pre-1.0 releases of Cheerio (which relied onhtmlparser2
), for those dealing with invalid markup (because htmlparser2
ismore forgiving), or for those operating in performance-critical situations(because htmlparser2
may be faster in some cases). Note that "more forgiving"means htmlparser2
has error-correcting mechanisms that aren't always a matchfor the standards observed by web browsers. This behavior may be useful whenparsing non-HTML content.
To support these cases, load
also accepts a htmlparser2
-compatible datastructure as its first argument. Users may install htmlparser2
, use it toparse input, and pass the result to load
:
// Usage as of htmlparser2 version 6:
const htmlparser2 = require('htmlparser2');
const dom = htmlparser2.parseDocument(document, options);
const $ = cheerio.load(dom);
If you want to save some bytes, you can use Cheerio's slim export, whichalways uses htmlparser2
:
const cheerio = require('cheerio/lib/slim');
Cheerio's selector implementation is nearly identical to jQuery's, so the API is very similar.
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
is typically the HTML document string.
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.
$('.apple', '#fruits').text();
//=> Apple
$('ul .pear').attr('class');
//=> pear
$('li[class=orange]').html();
//=> Orange
You can select with XML Namespaces but due to the CSS specification, the colon (:
) needs to be escaped for the selector to be valid.
$('[xml\\:id="main"');
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 html
utility functon:
cheerio.html($('.pear'));
//=> <li class="pear">Pear</li>
You may also render the text content of a Cheerio object using the text
static method:
const $ = cheerio.load('This is <em>content</em>.');
cheerio.text($('body'));
//=> This is content.
Once you have loaded a document, you may extend the prototype or the equivalent fn
property with custom plugin methods:
const $ = cheerio.load('<html><body>Hello, <b>world</b>!</body></html>');
$.prototype.logHtml = function () {
console.log(this.html());
};
$('body').logHtml(); // logs "Hello, <b>world</b>!" to the console
If you're using TypeScript, you should add a type definition for your new method:
declare module 'cheerio' {
interface Cheerio<T> {
logHtml(this: Cheerio<T>): void;
}
}
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
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.
Are you using cheerio in production? Add it to the wiki!
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.
Become a backer to show your support for Cheerio and help us maintain and improve this open source project.
This library stands on the shoulders of some incredible developers. A special thanks to:
• @FB55 for node-htmlparser2 & CSSSelect:Felix has a knack for writing speedy parsing engines. He completely re-wrote both @tautologistic's node-htmlparser
and @harry's node-soupselect
from the ground up, making both of them much faster and more flexible. Cheerio would not be possible without his foundational work
• @jQuery team for jQuery:The core API is the best of its class and despite dealing with all the browser inconsistencies the code base is extremely clean and easy to follow. Much of cheerio's implementation and documentation is from jQuery. Thanks guys.
• @visionmedia:The style, the structure, the open-source"-ness" of this library comes from studying TJ's style and using many of his libraries. This dude consistently pumps out high-quality libraries and has always been more than willing to help or answer questions. You rock TJ.
MIT
一.定义: cheerio是jquery核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对DOM进行操作的地方 二.简要介绍: 1.实例代码: var cheerio = require('cheerio'), $ = cheerio.load('<h2 class = "title">Hello world</h2>'); $('h2.title').text('He
这篇参考手册是对cheerio 官方文档 的中文翻译 cheerio是jquery核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对DOM进行操作的地方 简介 让你在服务器端和html愉快的玩耍 var cheerio = require('cheerio'), $ = cheerio.load('<h2 class = "title">Hello world</h2>')
简介 cheerio是jquery核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对DOM进行操作的地方 让你在服务器端和html愉快的玩耍 var cheerio = require('cheerio'), $ = cheerio.load('<h2 class = "title">Hello world</h2>'); $('h2.title').text('
cheerio-GitHub中文文档 官方对它的简介:为服务器特别定制的,快速,灵活,实施的jQuery核心实现 本文主要记录如何用cheerio来解析HTML抓取有效数据,也就是所谓的爬虫 # 安装 npm install cheerio # 使用 加载 var cheerio = require('cheerio'); let $ = cheerio.load(response, { igno
话不多说,直接上代码,代码目前只做到把图片src解析出来,并没有实现真正的下载,其中图片解析单独做出一个函数parseImageUrl,运行后,首先获取网页列表,然后每隔5秒获取列表中一个页面,同时获取页面中的所有图片 var request = require('request'); var cheerio = require('cheerio'); var artlist = []; var
climbThePage.js // (下载网页中的图片) // 用于发送http请求 const https = require('https') const http = require('http') // 用于提取网页中的img标签 const cheerio = require('cheerio') // 用于将http响应中的数据写到文件中 const fs = require('f
关于cheerio的api的学习笔记 我看的文档 因为这两周都在看Node.js实现爬虫的部分,对于cheerio这个模块的运用总是有问题出现,然后上网找了cheeriode API然后结合翻译看了一下。下面就是这一部分的学习笔记。 ps.这一部分是从loading 到 traversing 前言 cheerio是一款非常使用的nodejs第三方包,适用于nodejs端处理html(xml也可以)
const cheerio = require('cheerio'); const $ = cheerio.load(`<div tbinfo="ouid=3215204864" action-type="feed_list_item" diss-data="" mid="3833324051403226" class="WB_cardwrap WB_feed_type S_bg2 WB_feed
最近一段时间学习node,看到了爬虫,记录一下自己写的demo~~ const axios = require("axios"); //这里我使用了axios,vue中常用的,也可以自己封装别的 const cheerio = require("cheerio"); //引入了cheerio,用法和jq十分类似,不用自己写那些igs正则了,多的很 const express = require("
文章开头先介绍一下主角 cheerio cheerio是nodejs的抓取页面模块,为服务器特别定制的,快速、灵活、实施的jQuery核心实现。适合各种Web爬虫程序。 也就是用我们熟悉的jQuery语法,在服务器端对第三方网页的html文档进行操作。 步骤开始: 首先让我们先导入本篇文章的主角 const https = require('https'); const cheerio = r
cheerio 在一个元素基础上查询
问题内容: 我的硬盘驱动器上有一些html文件,我想使用jquery从中提取数据。使用cheerio可以做到吗?我已经尝试过为cheerio提供本地路径,但是它不起作用。我的一个想法是在节点中创建一个Web服务器,从html文件中读取内容,然后通过服务器将其通过管道传输到cheerio- 这会 问题答案: 输入的是html字符串,因此您需要自己阅读html内容:
本文向大家介绍nodejs爬虫初试superagent和cheerio,包括了nodejs爬虫初试superagent和cheerio的使用技巧和注意事项,需要的朋友参考一下 前言 早就听过爬虫,这几天开始学习nodejs,写了个爬虫https://github.com/leichangchun/node-crawlers/tree/master/superagent_cheerio_demo
首先:我是node的新手,也是编程的初学者。 我正在尝试用Express创建一个小型web应用程序,其唯一目标是从一个没有开放API的网站获取并重新格式化数据。 为了做到这一点,我决定学习刮痧,这把我带到了Cheerio和Request。 我以reddit为例来学习。本例的最终目标是收集首页上帖子的名称和href以及指向评论的url,然后进入该页面以刮取评论的数量。 下面是在GET请求中调用到/的
问题内容: 页面上有一个全局变量,其中包含一个我想为其设置刮板的对象。使用Node / Express /可能使用Cheerio的最佳方法是什么? 我了解Cheerio在遍历DOM方面的好处,但是我知道我要抓取的全局变量的名称,只需要按设定的时间表提取其信息即可 问题答案: Cheerio只是一个dom解析器,因此您不会访问任何javascriot或任何javascript生成的内容。 您需要像P
我的问题: 我正在构建一个包含Cheerio、Node.js和Google Cloud功能的Web刮板。 问题是我需要发出多个请求,然后在调用response.send()并因此终止函数之前将每个请求中的数据写入Firestore数据库。 我的代码需要两个循环:第一个循环使用我db中的URL,每个URL都发出一个单独的请求。第二个循环是Cheerio使用.each从DOM中刮取多行表数据,并对每一
目标 建立一个 lesson3 项目,在其中编写代码。 当在浏览器中访问 http://localhost:3000/ 时,输出 CNode(https://cnodejs.org/ ) 社区首页的所有帖子标题和链接,以 json 的形式。 输出示例: [ { "title": "【公告】发招聘帖的同学留意一下这里", "href": "http://cnodejs.org/t