muy*_*ueh 6 encoding utf-8 node.js
我是Node.js的新手,我正在尝试使用请求模型来删除网站,我遇到编码问题:目标网站使用big5作为编码,我希望将其转换为utf-8以下代码:
var Iconv = require('iconv').Iconv;
var fs = require('fs');
var big5_to_utf8 = new Iconv('big5', 'utf-8');
var buffer = big5_to_utf8.convert(fs.readFileSync('./test'));
console.log(buffer.toString());
Run Code Online (Sandbox Code Playgroud)
我怀疑这个问题可能是由于报废过程中的一些错误引起的,所以为了你的参考,我的报废代码:
var fs = require('fs');
var request = require('request');
var j = request.jar()
var cookie = request.cookie('ASPSESSIONIDCSDCTTSR=KDMMMIMDCCIHJIJFDKGEDFOH')
j.add(cookie)
request({
url: 'http://amis.afa.gov.tw/v-asp/v101r.asp',
method: "POST",
"Content-type": "application/x-www-form-urlencoded;",
jar:true,
encoding: 'utf-8',
form: {
mhidden1:false,
myy:101,
mmm:9,
mdd:25,
mpno:"FC",
mpnoname:"%ADJ%A5%CA++++",
B1:"%B6%7D%A9l%ACd%B8%DF",
}
}, function (error, response, body) {
console.log(body);
fs.writeFile("test", body);
});
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助.
编辑:
为了更具体地说明错误,以下是代码返回的内容:
<p align="center"><font color="#800080">?????s?????u???C??</font><em><font
size="4" color="#000080">[?W?@??]</font></em><font color="#800080">?^???e?@???J?????e???~???d??</font></p>
Run Code Online (Sandbox Code Playgroud)
这应该是它应该返回的:
<p align="center"><font color="#800080">??????????</font><em><font size="4" color="#000080">[???]</font></em><font color="#800080">??????????????</font></p>
Run Code Online (Sandbox Code Playgroud)
我还尝试使用iconv-lite而不是iconv,将函数调用替换为以下内容:
function (error, response, body) {
var bufferhelper = new BufferHelper();
bufferhelper.concat(body);
console.log(iconv.decode(bufferhelper.toBuffer(), 'Big5'));
});
Run Code Online (Sandbox Code Playgroud)
只得到:
<p align="center"><font color="#800080">?????????</font><em><font
size="4" color="#000080">[???]</font></em><font color="#800080">??????????????</font</p>
Run Code Online (Sandbox Code Playgroud)
小智 7
我iconv-lite用来解码big5到utf8.
你应该设置encoding:null这request将返回原始编码页.
这是示例代码.
var iconv = require('iconv-lite');
var request = require('request');
request({ url: 'http://amis.afa.gov.tw/v-asp/v101r.asp',encoding:null}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var str = iconv.decode(new Buffer(body), "big5");
console.log(str);
}
});
Run Code Online (Sandbox Code Playgroud)
回归是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>v101r</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="Microsoft Theme" content="none, default">
</head>
<body>
<p align="center">????!</p>
<p align="center"><font color="#800080">??????????</font><em><font
size="4" color="#000080">[???]</font></em><font color="#800080">??????????????</font></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我用node.js 0.10.20的RedHat EL 6.4和 iconv-lite 0.2.11,request 2.27.0