axiosでShift-JISなページをクロールする方法

http://bit.ly/1SS2bDp

↑のようなShift-JISなページをaxiosでクロールするときは、iconvを使い、transformResponseの段階で変換してしまうと良い。

github.com

import { Iconv } from 'iconv';
const sjis2utf8 = new Iconv('SHIFT_JIS', 'UTF-8//TRANSLIT//IGNORE');

axios.get('http://bit.ly/1SS2bDp', {
  responseType      : 'arraybuffer',
  transformResponse : [(data) => {
  return sjis2utf8.convert(data).toString();
  }],
})
.then((res) => {
  console.log(res.data);
});

//<title>Shift-JISですよ</title>
// <body>☆★☆?南無観世音菩薩?☆★☆</body>

↓にょうに、resを受け取ってから変換するのでは上手く行かなかった。

axios.get('http://bit.ly/1SS2bDp', {
  responseType : 'arraybuffer',
})
.then((res) => {
  console.log(sjis2utf8(res.data).toString());
});

// 何も表示されない