node.js cheerio

安装 cheerio

npm install cheerio
let cheerio = require('cheerio');
let $ = cheerio.load('<h2 class="title">Hello World</h2>');
console.log($.html());

$('h2.title').text('Hello Muzico!');
console.log($.html());

输出:

<html><head></head><body><h2 class="title">Hello World</h2></body></html>
<html><head></head><body><h2 class="title">Hello Muzico!</h2></body></html>
let cheerio = require('cheerio');
let $ = cheerio.load('<h2 class="title">Hello World</h2>');
console.log($.html());

$('h2').addClass('welcome');
console.log($.html());

输出:

<html><head></head><body><h2 class="title">Hello World</h2></body></html>
<html><head></head><body><h2 class="title welcome">Hello World</h2></body></html>