nodejs使用请求进行编码

编程入门 行业动态 更新时间:2024-10-26 06:31:31
本文介绍了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":'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:'www.bold.dk/tv/', encoding: null }, function(err, resp, body){ var bodyWithCorrectEncoding = iconv.decode(body, 'iso-8859-1'); console.log(bodyWithCorrectEncoding); } );

更多推荐

nodejs使用请求进行编码

本文发布于:2023-10-10 01:22:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!