【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.
相关推荐
中犇科技11 小时前
电商app源码系统推荐|开源 uniapp 商城系统
uni-app
2301_8156453813 小时前
html.
前端·html
ZC跨境爬虫14 小时前
跟着 MDN 学 HTML day_33:(Attr 接口与属性节点的深入理解)
前端·javascript·ui·html·音视频·html5
焦糖玛奇朵婷14 小时前
健身房预约小程序开发、设计
java·大数据·服务器·前端·小程序
海水冷却15 小时前
uniapp 实现直播功能的完整方案与实战指南
uni-app
wuxianda103016 小时前
Object-C/Swift/UniApp项目苹果商店上架3天极速解决方案汇报总结
ios·uni-app·objective-c·cocoa·苹果上架
Dragon Wu16 小时前
Taro v4.2.0 scss使用“@/xxx“的配置方法
前端·小程序·taro·scss
WKK_16 小时前
uniapp 微信小程序使用TextEncoder,arrayBufferToBase64
微信小程序·小程序·uni-app
不瘦80斤不改名17 小时前
HTML基础(一)
开发语言·前端·html
ZC跨境爬虫17 小时前
跟着 MDN 学 HTML day_32:(AbstractRange 抽象接口与 DOM 范围操作)
前端·javascript·ui·html·音视频