NodeJS http响应编码

发布于 2024-10-19 14:43:06 字数 571 浏览 5 评论 0原文

是否可以读取非 utf8 编码的网页?例如 windows-1251。 我尝试使用节点图标转换结果:

var convertedBody = new Iconv('windows-1251','utf-8').convert(responseBody));

但我得到异常:

Error: EILSEQ, Illegal character sequence.
    at IncomingMessage.<anonymous> (/root/nodejstest/test2.js:22:19)
    at IncomingMessage.emit (events.js:59:20)
    at HTTPParser.onMessageComplete (http.js:111:23)
    at Socket.ondata (http.js:1183:22)
    at Socket._onReadable (net.js:654:27)
    at IOWatcher.onReadable [as callback] (net.js:156:10)

谢谢!

Is in possible to read web page in non utf8 encoding? For example windows-1251.
I tried to convert result using node-iconv:

var convertedBody = new Iconv('windows-1251','utf-8').convert(responseBody));

But I get exception:

Error: EILSEQ, Illegal character sequence.
    at IncomingMessage.<anonymous> (/root/nodejstest/test2.js:22:19)
    at IncomingMessage.emit (events.js:59:20)
    at HTTPParser.onMessageComplete (http.js:111:23)
    at Socket.ondata (http.js:1183:22)
    at Socket._onReadable (net.js:654:27)
    at IOWatcher.onReadable [as callback] (net.js:156:10)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埖埖迣鎅 2024-10-26 14:43:06

这是您问题的有效解决方案。您必须首先使用 Buffer 并将字符串转换为二进制。

request({ 
uri: website_url,
method: 'GET',
encoding: 'binary'
}, function (error, response, body) {
    body = new Buffer(body, 'binary');
    conv = new iconv.Iconv('windows-1251', 'utf8');
    body = conv.convert(body).toString();
     }
});

Here is working solution to your problem. You have to use Buffer and convert your string to binary first.

request({ 
uri: website_url,
method: 'GET',
encoding: 'binary'
}, function (error, response, body) {
    body = new Buffer(body, 'binary');
    conv = new iconv.Iconv('windows-1251', 'utf8');
    body = conv.convert(body).toString();
     }
});
黄昏下泛黄的笔记 2024-10-26 14:43:06

查看 iconv-lite 库。
使用它你的代码可能如下所示:

var iconv = require('iconv-lite');
request(
    { 
        uri: website_url,
        method: 'GET',
        encoding: 'binary'
    },
    function(err, resp, body){
        body = iconv.decode(body, 'win1251');
    }
);

Take a look at the iconv-lite library.
Using it your code may look like this:

var iconv = require('iconv-lite');
request(
    { 
        uri: website_url,
        method: 'GET',
        encoding: 'binary'
    },
    function(err, resp, body){
        body = iconv.decode(body, 'win1251');
    }
);
轮廓§ 2024-10-26 14:43:06

Iconv 没有 windows-1251

您可以从 bnoordhuis/node-iconv 验证编码列表。

顺便说一句,来自维基百科:

Windows-1251 和 KOI8-R(或其乌克兰变体 KOI8-U)比 ISO 8859-5 更常用。

Iconv doesn't has windows-1251.

You can verify the list of encodings from bnoordhuis/node-iconv.

BTW, from wikipedia:

Windows-1251 and KOI8-R (or its Ukrainian variant KOI8-U) are much more commonly used than ISO 8859-5.

御弟哥哥 2024-10-26 14:43:06
const request = require('request');
const iconv = require('iconv-lite');

request({
    url: 'http://meta.ua',
    encoding: 'binary',
}, (err,res,body) => {
    if (err) throw err;

    var decoded = iconv.decode(res.body, 'win1251');

    console.log(decoded);
});
const request = require('request');
const iconv = require('iconv-lite');

request({
    url: 'http://meta.ua',
    encoding: 'binary',
}, (err,res,body) => {
    if (err) throw err;

    var decoded = iconv.decode(res.body, 'win1251');

    console.log(decoded);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文