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
最新推荐文章于 2024-06-07 20:52:36 发布