The Magus
網羅的にオブジェクトを生成してくれるnode_moduleを作りました。
npm install the-magus
網羅的にスクレイピングしたいときに便利です。
使用例
検索ワードの組み合わせを変えて、検索用のjsonデータを作成します。
実装
// 検索ワードの組み合わせを定義
const word = ['the-fool', 'json スクレイピング'];
const tech = ['node', 'node.js'];
const qiita = ['qiita', ''];
// テンプレートとするデータを定義
const templateData = {
data: [
['goto', 'https://www.google.co.jp'],
["type", "form[action='/search']", "$word $tech $qiita"], // $マークの後に変数名
["click", "form[action*='/search'] [type=submit]"],
["wait", "._NId h3 a"],
["snatch", "._NId h3 a", "innerText"]
]
};
const magus = new Magus({ word, tech, qiita }, templateData); // { word }はes6のshorhandです
magus.produce((product) => {
console.info(product);
});
実行結果
/*
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'the-fool node qiita' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'json スクレイピング node.js ' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'the-fool node qiita' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'json スクレイピング node.js ' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'the-fool node qiita' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'json スクレイピング node.js ' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'the-fool node qiita' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
{ data:
[ [ 'goto', 'https://www.google.co.jp' ],
[ 'type', 'form[action=\'/search\']', 'json スクレイピング node.js ' ],
[ 'click', 'form[action*=\'/search\'] [type=submit]' ],
[ 'wait', '._NId h3 a' ],
[ 'snatch', '._NId h3 a', 'innerText' ] ] }
*/
the-foolと併せて使うと凶悪ですね!
http://qiita.com/ito-p/items/29398c1385ad48fc71c3
注意
ご使用は自己責任でお願いします。