【uniapp小程序】使用cheerio去除字符串中的HTML标签并获取纯文本内容
参考资料
【博主:AIpoem】uniapp小程序 使用cheerio处理网络请求拿到的dom数据
cheerio文档:https://github.com/cheeriojs/cheerio/wiki/Chinese-README
安装
powershell
npm i cheerio [email protected]
引入
js
import * as cheerio from 'cheerio';
使用
js
const $ = cheerio.load('<div><h1 class="content">hello world!</h1><p class="text">This is a <strong>test</strong> paragraph one.</p><p>This is a <strong>test</strong> paragraph two.</p></div>');
const content = $('body').text();
console.log(content); // hello world!This is a test paragraph one.This is a test paragraph two.
const content2 = $('h1').text();
console.log(content2); // hello world!
const content3 = $('p').text();
console.log(content3); // This is a test paragraph one.This is a test paragraph two.
const content4 = $('p.text').text();
console.log(content4); // This is a test paragraph one.