node爬取html乱码
var http = require('http'), iconv = require('iconv-lite'); http.get("http://website.com/", function(res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { var decodedBody = iconv.decode(body, 'win1252'); // 或者gbk/gb2312等 console.log(decodedBody); }); });
在使用iconv.decode()解码之前,最初的资源已经通过body += chunk
解码了,发生了什么:
res.on('data', function(chunkBuffer) { body += chunkBuffer.toString('utf8'); });
如果你设置了res.setEncoding(‘utf8’);。那么和上面的转化是一样的。
对于最开始的问题,使用了两次解码(无论一次还是两次),都是错误的结果。几乎是不可能地回复最初的字节因为utf8转换是有损耗的(lossy)。所以即使是iconv.decode(new Buffer(body, 'utf8'), 'win1252')
也无济于事。
注意: theoretically, if you use ‘binary’ encoding to first decode to strings, then feed them to decode, you get the correct results. This is a bad practice because it’s slower, it’s mixing concepts and ‘binary’ encoding is deprecated.
这段不是很懂什么意思,反正使用’binary’编码是不赞成的。(二进制编码?)
解决方案
保持开始的buffer
并且使用iconv.decode
,如果有必要,可以使用Buffer.concat()
。
首先需要知道的是,在data事件的时候,所有的js字符串都是经过解码(utf8)的,我们就不需要手动解码了。
http.get("http://website.com/", function(res) { var chunks = []; res.on('data', function(chunk) { chunks.push(chunk); }); res.on('end', function() { var decodedBody = iconv.decode(Buffer.concat(chunks), 'win1252'); console.log(decodedBody); }); }); // Or, with iconv-lite@0.4 and Node v0.10+, you can use streaming support with `collect` helper http.get("http://website.com/", function(res) { res.pipe(iconv.decodeStream('win1252')).collect(function(err, decodedBody) { console.log(decodedBody); }); });
如果想要忽略警告:
iconv.skipDecodeWarning = true;
转自https://blog.csdn.net/real_bird/article/details/53309512