const cheerio = require('cheerio');
const response = await axios({
url: url,
headers: {
// ...
},
responseType: "arraybuffer", // 关键步骤
responseEncoding: "utf8",
});
let { data } = response
// 根据Content-Type头中的charset字段来确定编码判断是否是UTF8
const contentType = response.headers['content-type'];
const charsetMatch = contentType && contentType.match(/charset=([^;]+)/);
let $
if(charsetMatch){
$ = cheerio.load(data,{decodeEntities: false});
}else{
let utf8decoder = new TextDecoder("GBK"); // 关键步骤
let html = utf8decoder.decode(data);
console.log(html,8888)
$ = cheerio.load(html,{decodeEntities: false});
}
return $('body').html()
获取外部链接的html
最新推荐文章于 2025-12-04 17:59:42 发布
本文介绍了如何在Node.js中使用axios库获取响应数据时,根据Content-Type头中的charset进行UTF8或GBK编码转换,然后使用Cheerio解析HTML内容的过程。
2083

被折叠的 条评论
为什么被折叠?



