How to convert character encoding from CP932 to UTF-8 in nodejs javascript, using the nodejs-iconv module (or other solution)

后端 未结 3 926
天涯浪人
天涯浪人 2020-12-15 14:07

I\'m attempting to convert a string from CP932 (aka Windows-31J) to utf8 in javascript. Basically I\'m crawling a site that ignores the utf-8 request in the request header

3条回答
  •  伪装坚强ぢ
    2020-12-15 14:34

    I had same problem, but with CP1250. I was looking for problem everywhere and everything was OK, except call of request – I had to add encoding: 'binary'.

    request = require('request')
    Iconv  = require('iconv').Iconv
    
    request({uri: url, encoding: 'binary'}, function(err, response, body) {
        body = new Buffer(body, 'binary')
        iconv = new Iconv('CP1250', 'UTF8')
        body = iconv.convert(body).toString()
        // ...
    })
    

提交回复
热议问题