nodejs request iconv

在使用nodejs request抓取網頁時,如果需要進行轉碼,則須注意request的callback中body是string or buffer
對於Big5來說 如果轉成javascript string, 此時body在iconv(由big5=>utf8)會失敗。 javascript string假設input是utf8
需要強制body保留原始bytes

request({
url: ...,
method: 'GET',
encoding: null
}, function (err, resp, body){ //body is buffer now
});

在說明文件有提到

encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer.

此時再利用如iconv的工具進行編碼轉換

This entry was posted in nodejs. Bookmark the permalink.

Leave a Reply