Weird characters when using console.print cheerio + nodejs

后端 未结 1 858
小鲜肉
小鲜肉 2021-01-22 02:48

I\'m new to node.js and writing my first script to scrape some data.

Does anyone know why I\'m seeing weird characters with question marks inside them when using this co

1条回答
  •  伪装坚强ぢ
    2021-01-22 03:07

    Hey this is because of the encoding of the page you're requesting. To deal with encoding, you might want to use the module iconv-lite (https://github.com/ashtuchkin/iconv-lite) like that:

    var iconv = require('iconv-lite');
    
    var encoding = 'iso-8859-1'; // You might want to replace that with the encoding the page is using or auto detect it from the charset header
    
    request.get({url: .., headers:..., encoding:null}, function(err,res,body){
    
       var body1 = iconv.decode(body,encoding);
    
    }
    

    Have fun, this should work.

    0 讨论(0)
提交回复
热议问题