cheerio html方法中文被编码问题

如下代码:

const cheerio = require('cheerio')
const $ = cheerio.load('<html><head><title></title><meta charset="utf-8"/></head><body></body></html>')

$('body').append('<div class="content">我是内容</div>')

console.log($.html());

输出内容为:

会发现中文被编码,如果不想被编码,则在load方法中增加decodeEntities参数即可,如下:

const $ = cheerio.load('<html><head><title></title><meta charset="utf-8"/></head><body></body></html>',{decodeEntities:false})

输出内容就会变为:

<html><head><title></title><meta charset="utf-8"></head><body><div class="content">我是内容</div></body></html>

Leave a Reply

Your email address will not be published. Required fields are marked *