node Cheerio 获取script脚本里的数据

const cheerio = require('cheerio');
const $ = cheerio.load(html); // your html
//如果有多少script脚本标签使用循环来获取
$('script').map(function(i, el) {
// 建议这里打印$(this) 因为this的对象可能不同
//因为存在script标签为空的数据, 所以判断children长度是否大于0
console.log("ffffffffff", $(this)[0].children[0])
//如果要获取script标签的属性, 例如 type
console.log($(this)[0].attribs['type'])
});
 

 

posted @ 2020-09-16 15:37  开始战斗  阅读(1824)  评论(0编辑  收藏  举报