N
N
Nikolai Antonov2016-09-14 00:45:51
JavaScript
Nikolai Antonov, 2016-09-14 00:45:51

How to convert from windows-1251 to utf-8?

I'm trying to parse from a site that has an encoding:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">

Using the iconv module, I try to recode and still get krakozyabry
:
var request = require('request');
var cheerio = require('cheerio');
var Buffer = require('buffer').Buffer;
var Iconv  = require('iconv').Iconv;
var iconv = new Iconv('CP1251','UTF-8');

request(url, function(error, response, body){
        var $ = cheerio.load(body);
        var str = $('#selector').text();

        console.log(iconv.convert(str).toString()); // кракозябры: пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ 
});

PS When I try to send an element with English text, everything is correct - English text with the correct encoding is displayed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-09-14
@my-nickname

Try
installing like this: npm install iconv-lite
iconv = require('iconv-lite');
result = iconv.encode(iconv.decode(new Buffer(body, 'binary'), 'win1251'), 'utf8'));
I haven't checked it myself yet, it wasn't before.

I
Ilya, 2016-09-14
@glebovgin

Here is a suggested solution:

body = new Buffer(body, 'binary');
conv = new iconv.Iconv('windows-1251', 'utf8');
body = conv.convert(body).toString();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question