黑白梦黑白梦

toggle navtoggle nav
  • 文章
  • 专栏
  • 文章
  • 专栏

cheerio 类 jq 语法进行访问与操作 html 文本

发布于 2021-09-19, 更新于 2023-04-08

对于 html 文本,使用 cheerio 进行数据提取,通过 jq 的语法就可以进行数据的操作。

https://github.com/cheeriojs/cheerio

npm i -S cheerio
npm i -D @types/cheerio

使用类似 jq 的语法就可以查看或操作 html 字符串。

import * as cheerio from 'cheerio';
const $ = cheerio.load('<ul id="fruits">...</ul>');

$('.apple', '#fruits').text(); // Apple
$('ul .pear').attr('class'); // pear
$('li[class=orange]').html(); // Orange
本文收录于专栏

一些好用的 npm 前端开源库

收集一些好用的前端开源库,主要是 npm 包

©2015-2026 黑白梦 粤ICP备15018165号

联系: heibaimeng@foxmail.com