Cheerio โ
Node.js ็ฏๅขไธ็ HTML/XML ่งฃๆๅบ๏ผๆไพ jQuery ้ฃๆ ผ็้ๆฉๅจ APIใ
feature โ
- ้ๅบฆๆๅฟซ
- ้ๅ็ฌ่ซใ้ๆ้กต้ข่งฃๆ
- ้ๅ็ปๆๅๆๅ
WARNING
ๅฎไธๆง่ก JSใไธๆธฒๆ้กต้ข๏ผๅ ๆญค๏ผ
install โ
bash
npm install cheeriousage โ
js
import * as cheerio from 'cheerio';js
const cheerio = require('cheerio');API โ
js
$('h1')
$('.item')
$('#main')
$('ul li:first-child')js
$('h1').text()js
$('img').attr('src')js
$('li').each((i, el) => {
console.log($(el).text());
});js
$('title').text('New Title');
$('body').append('<p>Hello</p>');js
$.html()example โ
js
import * as cheerio from 'cheerio';
const html = `
<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>
`;
const $ = cheerio.load(html);
console.log($('.apple').text()); // Apple
console.log($('#fruits li').length); // 3