安装cheerio
npm install cheerio
git: https://github.com/cheeriojs/cheerio/wiki/Chinese-README引入cheerio
import * as cheerio from 'cheerio'
- 使用
uni.request({
url: 'http://xxx.com',
success: (res) => {
const dom = cheerio.load(res.data, {
_useHtmlParser2: true
})
const target = dom('.myClass p span')[1]?.children[0]?.data
console.log(target)
}
})
cheerio.load获取了整个网页的dom,在dom中匹配目标的class类名,如dom('.myClass p span'),可获取目标的值及其他内容