LoginSignup
13
17

More than 5 years have passed since last update.

Amazon Product APIをPythonで叩く簡単な方法

Posted at

自分でAPIを叩いてもいいんだけど,ドキュメントが読みづらいし,結局リクエストに何のパラメーターが必要なのかパッと書いてないですし,なんか自分でハッシュ化したSigunatureをURLに付けたりしないといけなくて面倒なので,便利なライブラリを使いましょう.

python-amazon-simple-product-api

超便利です.
インストールなどはREADME読めばすぐに分かります.

実際に実行してみるとこんな感じです.
regionを指定してやらないと海外のストアを検索しているのか,商品が見つからないので,region="JP"を忘れないようにしてください.

In [9]: amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOCIATE_ID, region="JP")
In [10]: product = amazon.lookup(ItemId='B00E7N623K')
In [11]: product.__dict__
Out[11]:
{'api': <amazon.api.AmazonAPI at 0x1037e9cf8>,
 'aws_associate_tag': 'アソシエイトID',
 'parent': None,
 'parsed_response': <Element {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Item at 0x1035b9e08>,
 'region': 'JP'}

In [12]: product.parsed_response
Out[12]: <Element {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Item at 0x1035b9e08>

In [13]: product.parsed_response.ItemAttributes
Out[13]: <Element {http://webservices.amazon.com/AWSECommerceService/2013-08-01}ItemAttributes at 0x10382e508>

ライブラリの中身ではlxmlでxmlのレスポンスをパースしている気がします.
実際,上記の実行例の中でもlxmlのElementインスタンスが入れ子になって含まれていますね.

長々とAPIの公式ドキュメント読みましたが,明らかにこれが楽です.

13
17
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
13
17