dayjournal memo

Total 974 articles!!

Node.js #010 – Amazon APIで検索結果を取得

Yasunori Kirimoto's avatar

Node.jsでAmazon APIで検索結果を取得したい場合は下記のように記述します。


まず、Amazon APIを利用するにはアカウント登録が必要になります。

Amazon Product Advertising API

node.js_010_01


下記サイトにAPIキーの取得方法が詳しく記載してあるので参考になります。 AjaxTower


APIキーを取得できたら次に、npmでapacをインストールする必要があります。



npm install apac

script.js


var OperationHelper = require('apac').OperationHelper;
var cheerio = require('cheerio');

var opHelper;
var title;
var $;

    opHelper = new OperationHelper({
        awsId:     'ここに入力',
        awsSecret: 'ここに入力',
        assocId:   'ここに入力',
        endPoint: 'ecs.amazonaws.jp'
    });

    opHelper.execute('ItemSearch', {
        'SearchIndex': 'Books',
        'BrowseNode': 465610,
        'Keywords': 'GIS',
        'ResponseGroup': 'Small',
        'Sort': 'salesrank'
    }, 

    function(err, results, xml) {
        if (err) { console.log("error"); return; }
        $ = cheerio.load(xml);
        $("Items > Item").each(function(idx, item) {
            title = $(item).find("Title").text();
            console.log(title);
        });
    })

script.jsを実行すると下記のように取得したAPIがコマンドプロンプトに表示されます。

node.js_010_02


APIキーの設定 : アクセスキーID , シークレットアクセスキー , アソシエイトID


    opHelper = new OperationHelper({
        awsId:     'ここに入力',
        awsSecret: 'ここに入力',
        assocId:   'ここに入力',
        endPoint: 'ecs.amazonaws.jp'
    })

検索条件の設定 : SearchIndex: カテゴリー , BrowseNode: 詳細なカテゴリー , Keywords: 検索文字 ResponseGroup: 取得情報の種類 , Sort: 並び順


    opHelper.execute('ItemSearch', {
        'SearchIndex': 'Books',
        'BrowseNode': 465610,
        'Keywords': 'GIS',
        'ResponseGroup': 'Small',
        'Sort': 'salesrank'
    }, 
    

指定情報を抽出 :


    function(err, results, xml) {
        if (err) { console.log("error"); return; }
        $ = cheerio.load(xml);
        $("Items > Item").each(function(idx, item) {
            title = $(item).find("Title").text();
            console.log(title);
        });
    })


book

Q&A


  • 参考文献
[![JS+Node.jsによるWebクローラー/ネットエージェント開発テクニック](http://ecx.images-amazon.com/images/I/61DHJGf1uSL._SL160_.jpg)](http://www.amazon.co.jp/exec/obidos/ASIN/4883379930/dayjournal-22/ref=nosim/)
[JS+Node.jsによるWebクローラー/ネットエージェント開発テクニック](http://www.amazon.co.jp/exec/obidos/ASIN/4883379930/dayjournal-22/ref=nosim/)