使用console.print时奇怪的字符cheerio + nodejs

问题描述:

我是新来的node.js并编写我的第一个脚本来抓取一些数据。使用console.print时奇怪的字符cheerio + nodejs

有没有人知道为什么我使用这段代码时看到里面带有问号的怪异字符?

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; 

这里的输出我看到:

http://snag.gy/eQF1Y.jpg

谢谢!

Anthony

嘿,这是因为你正在请求的页面的编码。 为了应对编码,你可能想使用该模块的iconv - 精简版(https://github.com/ashtuchkin/iconv-lite)这样的:

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); 

} 

玩得开心,这应该工作。

+0

谢谢!这个解决方案对我有用:) – Anthony