【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.
相关推荐
祝余呀5 小时前
HTML初学者第三天
前端·html
ModyQyW8 小时前
用 AI 驱动 wot-design-uni 开发小程序
前端·uni-app
zhanshuo12 小时前
不依赖框架,如何用 JS 实现一个完整的前端路由系统
前端·javascript·html
火柴盒zhang12 小时前
websheet在线电子表格(spreadsheet)在集团型企业财务报表中的应用
前端·html·报表·合并·spreadsheet·websheet·集团财务
HERR_QQ15 小时前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
孤水寒月19 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀19 小时前
html初学者第一天
前端·html
耶啵奶膘21 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
racerun1 天前
小程序导航设置更多内容的实现方法
小程序
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源