router.get(’/api/public/test’, function (req,res) { var request = require(‘request’); var cheerio = require(‘cheerio’); var fs = require(‘fs’); var url = ‘http://www.umei.cc/’; var iconv = require(‘iconv-lite’); var options = { method: ‘GET’, url:url, encoding:‘utf8’ }; request(options, function (error, res2, body) { if (!error && res2.statusCode == 200) { var $ = cheerio.load(body); var li = $(‘DIV[class=t]’); var arr = []; li.each(function (index, ele) { var a = $(this).children()[1]; var href = $(a).attr(‘href’); var title = $(a).attr(‘title’); var src = $(‘img’, a).attr(‘src’); console.log(title) var obj = { src: src, href: ‘http://www.umei.cc’ + href, title: title }; arr.push(obj); }); //console.log(“arr:”, arr); res.send(arr) } });
});
比较想知道 如何 re.send 发送的中文不乱码 iconv这个 东西 都不知道在哪用
https://www.npmjs.com/package/iconv-lite#extend-node-js-own-encodings
// After this call all Node basic primitives will understand iconv-lite encodings.
// 调用完该方法,所有 node 的内置原生对象会明白 iconv-lite 的编码。换言之 iconv-lite 会增加原生的编码类型。
iconv.extendNodeEncodings();
// External modules are also supported (if they use Node primitives, which they probably do).
// 同时支持外部模块(如果类库使用NODE原生对象,一般情况下都是使用原生对象)。简单来讲你反正调用上面那个方法,就在APP的范围内全局扩展编码类型。
request = require('request');
request({
url: "http://github.com/",
encoding: "cp932"
});
@klesh 感谢大神!!!!!已经搞定了