时间:2021-07-01 10:21:17 帮助过:9人阅读
var fs = require("fs"); var iconv=require("iconv-lite"); var request=require("request"); var txt=fs.readFileSync("./a.txt","utf8"); iconv.decode(txt,"gbk") request("http://jd.com",function(err,res,body){ console.log(iconv.decode(body,"gbk")) });
上面输出的结果还是乱码,并不能解码gbk 先最新的iconv不支持string解码了
查看最新官方文档下面是正确的解码方式
var fs = require("fs"); var iconv=require("iconv-lite"); var request=require("request"); var txt=fs.readFileSync("./a.txt"); iconv.decode(txt,"gbk") request("http://jd.com") .pipe(iconv.decodeStream('gbk')) .collect(function(err, decodedBody) { fs.writeFile("./jd.txt",decodedBody); });