【uniapp小程序】使用cheerio去除字符串中的HTML标签并获取纯文本内容

【uniapp小程序】使用cheerio去除字符串中的HTML标签并获取纯文本内容

参考资料

【博主:AIpoem】uniapp小程序 使用cheerio处理网络请求拿到的dom数据
cheerio文档:https://github.com/cheeriojs/cheerio/wiki/Chinese-README

安装

npm i cheerio --@0.22.0

引入

import * as cheerio from 'cheerio';

使用

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值