I am new to Node.js, and I am trying to use the request model to scrap a website, I am having problem with the encoding: the target website is using big5 as encoding, and I wished to turn it to utf-8 with the following code:
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());
I doubt that the problem might be caused due to some wrong in the scrapping process, so for your reference, my code for scrapping:
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);
});
Really appreciate your help.
EDIT:
To be more specific to the error, the following are what the code returns:
<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>
This is what it should return:
<p align="center"><font color="#800080">請使用瀏覽器工具列中</font><em><font size="4" color="#000080">[上一頁]</font></em><font color="#800080">回到前一輸入條件畫面繼續查詢</font></p>
I also tried to use iconv-lite instead of iconv, replacing the function call to the following:
function (error, response, body) {
var bufferhelper = new BufferHelper();
bufferhelper.concat(body);
console.log(iconv.decode(bufferhelper.toBuffer(), 'Big5'));
});
Only to get:
<p align="center"><font color="#800080">�濆詉胬胬譃胬舚胬</font><em><font
size="4" color="#000080">[抝胬]</font></em><font color="#800080">䒷胬蓚胬鸜胬胬蓚胬趦胬胬</font</p>
Might I suggest my codepage library:
yields
I use
iconv-lite
to decode big5 to utf8.And you should set
encoding:null
thatrequest
will return raw encoding page.This is sample code.
And return is
I use
node.js 0.10.20
onRedHat EL 6.4
andiconv-lite 0.2.11
,request 2.27.0