使用nodejs的request包请求中文gb2312网页时会乱码,默认:
1 var request = require('request');
2 request(url, function(error, response, body) {
3 if (!error && response.statusCode == 200) {
4 console.log(body);
5 }
6 });
需要进行转码:
var request = require('request');
var Iconv = require('iconv-lite');
request({
encoding: null,
url: url
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(Iconv.decode(body, 'gb2312').toString());
}
});