【uniapp小程序】使用cheerio去除字符串中的HTML标签并获取纯文本内容

【uniapp小程序】使用cheerio去除字符串中的HTML标签并获取纯文本内容

参考资料

【博主:AIpoem】uniapp小程序 使用cheerio处理网络请求拿到的dom数据
cheerio文档:https://github.com/cheeriojs/cheerio/wiki/Chinese-README

安装

powershell 复制代码
npm i cheerio --@0.22.0

引入

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.
相关推荐
anOnion1 天前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
米丘2 天前
微前端之 Web Components 完全指南
微服务·html
m0_526119405 天前
iconfont我修改好颜色,但是在小程序项目是黑色的
小程序
用户6990304848755 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_5 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Metaphor6925 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
2601_956743685 天前
2026 上海小程序开发甄选:源码、云函数、跨端兼容技术评判
小程序·开发经验·上海
IT_张三5 天前
CSDN-项目分享-暑期备考小程序
小程序
a1117765 天前
“黑夜流星“个人引导页 网页html
java·前端·html
JieE2125 天前
手把手带你用纯 CSS 实现一个 3D 旋转魔方,这些前端基础你能打几分?
前端·css·html