nodejs使用请求进行编码 [英] nodejs encoding using request

查看:196
本文介绍了nodejs使用请求进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  request.get({
uri :'http://www.bold.dk/tv/',
encoding:text / html; charset ='charset = utf-8'
},
function (err,resp,body){
console.log(body);
}
);

无论我做什么,丹麦字符的编码都是不对的。



任何想法?

解决方案

您可以使用iconv(lite)来转换。您还需要通过将encoding属性设置为null来请求不主动将编码设置为UTF-8的默认值。因此,您应该:

  var iconv = require('iconv-lite'); 
request.get({
uri:'http://www.bold.dk/tv/',
encoding:null
},
function(err ,resp,body){
var bodyWithCorrectEncoding = iconv.decode(body,'iso-8859-1');
console.log(bodyWithCorrectEncoding);
}
);


I am trying to get the correct encoding with request.

request.get({
    "uri":'http://www.bold.dk/tv/',
    "encoding": "text/html;charset='charset=utf-8'"
  },
  function(err, resp, body){    
    console.log(body);
  }
);

No matter what I do the encoding of the danish chars are not right.

Any thoughts?

解决方案

You can use iconv (lite) to convert this. You also need to tell request not to actively set the encoding to the default of UTF-8 by setting the encoding property to null. Therefore you should do:

var iconv = require('iconv-lite');
request.get({
    uri:'http://www.bold.dk/tv/',
    encoding: null
  },
  function(err, resp, body){    
    var bodyWithCorrectEncoding = iconv.decode(body, 'iso-8859-1');
    console.log(bodyWithCorrectEncoding);
  }
);

这篇关于nodejs使用请求进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆