【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.
相关推荐
1登峰造极5 小时前
uniapp 运行安卓报错reportJSException >>>> exception function:createInstanceContext, exception:white screen
android·java·uni-app
ヤ鬧鬧o.5 小时前
多彩背景切换演示
前端·css·html·html5
结衣结衣.6 小时前
protobuf介绍与快速上手
java·服务器·html
_OP_CHEN8 小时前
【前端开发之CSS】(三)CSS 常用元素属性宝典(上):从字体到文本,手把手教你打造高颜值网页!
前端·css·html·网页开发·文本属性·字体属性·页面美化
滴水未满9 小时前
uniapp的组件
uni-app
咖啡の猫9 小时前
微信小程序页面事件
微信小程序·小程序·notepad++
咖啡の猫11 小时前
微信小程序网络数据请求
网络·微信小程序·小程序
object not found11 小时前
基于uniapp开发小程序自定义顶部导航栏状态栏标题栏
前端·javascript·小程序·uni-app
松涛和鸣12 小时前
62、IIC通信解析
服务器·arm开发·单片机·嵌入式硬件·html
总爱写点小BUG13 小时前
VU-Icons:打造极致体验的 Vue3 & UniApp 双端 SVG 图标库
uni-app·vue·组件库·图标组件库