【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.
相关推荐
speedoooo9 小时前
在现有App里嵌入一个AI协作者
前端·ui·小程序·前端框架·web app
影子打怪9 小时前
uniapp项目中,通过renderjs的方式展示地图,及其标点、轨迹展示、轨迹回放
uni-app
iOS阿玮11 小时前
想偷懒购买现成的应用,结果一更新就遇到了4.3a!
uni-app·app·apple
HashTang12 小时前
【AI 编程实战】第 4 篇:一次完美 vs 五轮对话 - UnoCSS 配置的正确姿势
前端·uni-app·ai编程
万岳科技系统开发16 小时前
私域直播小程序源码的整体架构设计与实现思路
学习·小程序
汤姆yu18 小时前
基于微信小程序的驾校预约与学习系统
学习·小程序·驾校预约
苏打水com18 小时前
第十五篇:Day43-45 前端性能优化进阶——从“可用”到“极致”(对标职场“高并发场景优化”需求)
前端·css·vue·html·js
苏打水com18 小时前
第十六篇:Day46-48 前端安全进阶——从“漏洞防范”到“安全体系”(对标职场“攻防实战”需求)
前端·javascript·css·vue.js·html
speedoooo19 小时前
未来的App不再需要菜单栏?
前端·ui·容器·小程序·web app
猿究院_xyz19 小时前
微信小程序与echarts联动安卓真机测试出现黑色阴影
前端·javascript·微信小程序·小程序·echarts