【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.
相关推荐
小时前端17 小时前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
willow18 小时前
html5基础整理
html
Mr_li1 天前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
anyup2 天前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Mintopia3 天前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia3 天前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
anOnion3 天前
构建无障碍组件之Radio group pattern
前端·html·交互设计
不爱说话郭德纲3 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
前端Hardy3 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
前端Hardy5 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html