2014-05-22 44 views
1

我是新來的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

回答

6

嘿,這是因爲你正在請求的頁面的編碼。 爲了應對編碼,你可能想使用該模塊的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