pythonでAmazon Product APIにアクセスするライブラリ bottlenoseを触ってみたメモ


以前amazon product apipythonでアクセスするのにpython-amazon-product-apiを触ってみたんですが、
bottlenoseという別のライブラリがあると知ったのでそちらも試してみました。

python-amazon-product-apiを触ってみたメモ
http://d.hatena.ne.jp/yuheiomori0718/20111212/1323697894

インストール

pip install bottlenose
pip install BeautifulSoup

xmlをパースする機能は持ってないみたいので別途BeautifulSoupを使います。

準備

AWS_ACCESS_KEY、SECRET_KEY、ASSOCIATE_TAGはamazon product advertising APIのアカウントを作成すると取得できます

https://affiliate.amazon.co.jp/gp/advertising/api/detail/main.html

ASSOCIATE_TAGは*****-22のようなアカウントIDを指定します

AWS_ACCESS_KEY、SECRET_KEYの確認方法はこちら

https://affiliate.amazon.co.jp/gp/associates/help/t126/a5?ie=UTF8

使い方

sample.py

# coding=utf-8
from bottlenose import api
from bs4 import BeautifulSoup

AMAZON_ACCESS_KEY_ID="********************"
AMAZON_SECRET_KEY="****************************************"
AMAZON_ASSOC_TAG="****-22"

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region="JP")

def item_search(keywords, search_index="Books", item_page=1):
    response = amazon.ItemSearch(SearchIndex=search_index, Keywords=keywords, ItemPage=item_page, ResponseGroup="Large")
    soup = BeautifulSoup(response)
    return soup.findAll('item')

if __name__ == '__main__':

    for item in item_search('python'):
        title = item.find('title').text
        author = item.find('author').text
        asin = item.find('asin').text
        print("%s (%s) - %s" % (title, author, asin))


結果

Pythonスタートブック (辻 真吾) - 4774142298
エキスパートPythonプログラミング (Tarek Ziade) - 4048686291
初めてのPython 第3版 (Mark Lutz) - 4873113938
Python入門―2&3対応 (細田 謙二) - 4798026557
Pythonプロフェッショナルプログラミング (ビープラウド) - 4798032948
みんなのPython (柴田 淳) - 479733665X
みんなのPython 改訂版 (柴田 淳) - 4797353953
Python クックブック 第2版 (Alex Martelli) - 4873112761
Python ポケットリファレンス (Pocket Reference) (柏野 雄太) - 4774138053
Pythonチュートリアル 第2版 (Guido van Rossum) - 487311442X

python-amazon-product-apiよりもシンプルで分かりやすいと思います。