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

我是新来的node.js和写我的第一个脚本来刮一些数据。

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

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

谢谢!

安东尼

嘿,这是因为你要求的页面的编码。 要处理编码,你可能想要使用模块iconv-lite( https://github.com/ashtuchkin/iconvlite ):

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

玩得开心,这应该工作。