I got the product name and the lowest price from the Amazon site using the API, so make a note of it.
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='******' #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
Recommended Posts