PythonでAmazon APIを使う方法

Pocket

Pythonで「PA-API v5」を使う方法を簡単にまとめます。Macの場合です。

まず大前提としてAmazonアソシエイトの紹介料が直近30日以内に発生していないとAPIは使えません。
私も最近、紹介料がなく、使えませんでしたが、3日前にようやく発生し、また使えるようになりました。

アクセスキーの取得

次の通り取得します。

アソシエイト・セントラルのツールのドロップダウンメニューにある「Product Advertising API」で、既存のアクセスキーを確認したり新しいアクセスキーを生成することができます。

準備

python-amazon-paapiをインストールします。
python-amazon-paapi · PyPI

コード

{key}にはアクセスキーIDをセットします。英大文字、数字からなる文字列です。
{secret}にはシークレットアクセスキーをセットします。英大文字、英小文字、数字、記号からなる文字列です。
{tag}にはアソシエイトIDをセットします。私の場合は末尾に「-22」が付いています。

keywordsとして「新宿鮫 大沢在昌」を指定しています。
item_countとして「3」をセットしています。検索結果が3件返ってきます。

from amazon.paapi import AmazonAPI

def main():
  key = "{key}"
  secret = "{secret}"
  tag = "{tag}"
  country = "JP"
  amazon = AmazonAPI(key, secret, tag, country)

  keywords = "新宿鮫 大沢在昌"
  item_count = 3

  products = amazon.search_products(item_count = item_count, keywords = keywords)

  for product in products:
    print("asin", product.asin)
    print("title", product.title)
    print("image large", product.images.large)
    print("price velue", product.prices.price.value)
    print("price currency", product.prices.price.currency)
    for c in product.info.contributors:
      print("role", c.role)
      print("name", c.name)
    if product.product.features:
      for f in product.product.features:
        print("feature", f)
    if product.product.formats:
      for f in product.product.formats:
        print("format", f)

if __name__ == '__main__':
  main()

出力結果

asin B081C69J55
title 暗約領域 新宿鮫11
image large https://m.media-amazon.com/images/I/51lTT36khlL.jpg
price velue 1540.0
price currency JPY
role 著
name 大沢 在昌
format Kindle本
asin B07FNMJB3K
title 新宿鮫~新宿鮫1 新装版~ (光文社文庫)
image large https://m.media-amazon.com/images/I/61xXkl9StuL.jpg
price velue 770.0
price currency JPY
role 著
name 大沢 在昌
format Kindle本
asin B07FPN64CN
title 絆回廊 新宿鮫10~新装版~ (光文社文庫)
image large https://m.media-amazon.com/images/I/51o9r5wq04L.jpg
price velue 880.0
price currency JPY
role 著
name 大沢 在昌
format Kindle本

[ 2020年8月19日 | カテゴリー: Python, デジタル | タグ: , ]

« | »

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

送信してください。


タグ

カテゴリー

最近の投稿

最近のコメント

固定ページ

アーカイブ

stabucky

写真

メタ情報