Weird characters when using console.print cheerio + nodejs

做~自己de王妃 提交于 2019-12-02 03:07:01

问题


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 code?

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();

var url = 'http://www.ebay.co.uk/csc/all-you-ever-want/m.html?LH_Complete=1&_ipg=50&_since=15&_sop=13&LH_FS=1&=&rt=nc&LH_ItemCondition=3';

request(url, function (error, response, html) {
  if (!error) {

    console.log(html);
    var $ = cheerio.load(html);

    $('.vip').each(function (i, element) {
      var link = $(this).text();
      console.log(link);
    });

  }
});

app.listen(process.env.PORT, process.env.IP)
console.log(process.env.PORT);
exports = module.exports = app;

Here's the output I see:

Thank you!

Anthony


回答1:


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.



来源:https://stackoverflow.com/questions/23805566/weird-characters-when-using-console-print-cheerio-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!