[PYTHON] Get product name and lowest price using Amazon Product Advertising API

Purpose

I got the product name and the lowest price from the Amazon site using the API, so make a note of it.

Assumed environment

Advance preparation

pip install bottlenose
pip install BeautifulSoup

code

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

AWS_ACCESS_KEY_ID='******' #Obtained from the management console
AWS_SECRET_ACCESS_KEY='******' #Obtained from the management console
AWS_ASSOCIATE_TAG='******-22' #Associate (affiliate) registration required

SearchIndex="Books" #Reference https://images-na.ssl-images-amazon.com/images/G/09/associates/paapi/dg/index.html?JPSearchIndexParamForItemsearch.html
Keywords="Python" #Search keyword

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)

#Get the number of result pages
response = amazon.ItemSearch(
             SearchIndex=SearchIndex,
             Keywords=Keywords,
             ResponseGroup="ItemIds",
             ErrorHandler=error_handler)
soup=BeautifulSoup(response)
totalpages=int(soup.first('totalpages').text)
print "totalPages=",totalpages

#Get each page
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

I understand. I didn't understand.

Recommended Posts

Get product name and lowest price using Amazon Product Advertising API
Get Amazon data using Keep API # 1 Get data
Get data using Ministry of Internal Affairs and Communications API
Predict gender from name using Gender API and Pykakasi in Python
Get the weather using the API and let the Raspberry Pi speak!
Aggregate and analyze product prices using Rakuten Product Search API [Python]
Get Salesforce data using REST API
Get news from three major mobile companies using Django and the News API
I tried using Twitter api and Line api
Buy and sell cryptocurrencies using Zaif API
Get mail using Gmail API in Java
Get Youtube data in Python using Youtube Data API
Get the hierarchy name using the OpenMaya iterator
[Python] Get product information such as ASIN and JAN with Amazon PA-API ver5.0
Extract the product name and price from the product list in the Yodobashi.com purchase statement email.