LoginSignup
35
32

More than 5 years have passed since last update.

Amazon Product Advertising APIを使って商品名と最低価格を取得する

Posted at

目的

AmazonサイトからAPIで商品名と最低価格を取得したのでメモしておく。
※AMAZONのAPI仕様、ライブラリの仕様が変更になるかもしれない。以下は2015年3月10日時点で動作したもの。

想定環境

  • Python 2.7
  • AmazonのAPIにはbottlenoseこれを使用。
  • XMLにはBeautifulSoup 3.2.1 ここを使用。

事前準備

  • Amazonマネージメントコンソールからアクセスキーを取得。(Security Credentials->Access Keys)
  • アソシエイト(アフィリエイト)の登録。
  • bottlenoseとBeautifulSoupインストール。
pip install bottlenose
pip install BeautifulSoup

コード

# -*- coding: utf-8 -*-
import bottlenose
from BeautifulSoup import BeautifulSoup
import random
import time
from urllib2 import HTTPError

AWS_ACCESS_KEY_ID='******' #マネージメントコンソールから取得
AWS_SECRET_ACCESS_KEY='******' #マネージメントコンソールから取得
AWS_ASSOCIATE_TAG='******-22' #アソシエイト(アフィリエイト)の登録が必要

SearchIndex="Books" #参照 https://images-na.ssl-images-amazon.com/images/G/09/associates/paapi/dg/index.html?JPSearchIndexParamForItemsearch.html
Keywords="Python" #検索キーワード

def error_handler(err):
  ex = err['exception']
  if isinstance(ex, HTTPError) and ex.code == 503:
    time.sleep(random.expovariate(0.1))
    return True

amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG,Region="JP",ErrorHandler=error_handler)

#結果ページ数取得
response = amazon.ItemSearch(
             SearchIndex=SearchIndex,
             Keywords=Keywords,
             ResponseGroup="ItemIds",
             ErrorHandler=error_handler)
soup=BeautifulSoup(response)
totalpages=int(soup.first('totalpages').text)
print "totalPages=",totalpages

#各ページ取得
for page in range(totalpages) :
  print "="*20,"page",page+1
  if page >= 10: # max 10 pages
    break
  response = amazon.ItemSearch(
               SearchIndex=SearchIndex,
               Keywords=Keywords,
               ResponseGroup="Small,OfferSummary",
               ItemPage=page+1,
               ErrorHandler=error_handler)
  soup=BeautifulSoup(response)
  items = soup.findAll('item')
  for item in items:
    print item.title.text ,
    if item.offersummary and item.offersummary.lowestnewprice:
      print item.offersummary.lowestnewprice.formattedprice.text,
    print

分かったこと。分からなかったこと。

  • APIの呼出し回数の制限があるので、呼出し失敗時にはsleepしてリトライする。
  • 検索は最大100件(10件×10ページ)まで。(2011年ごろに設定された制約)
  • 検索時のデフォルトは新品のみのはずが、中古品まで含まれてしまう。(新品価格がうまく取れない場合がある。)
35
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
32