Searching for books with the Amazon Product Advertising API – Python

Question:

tl;dr: I am using the Amazon Product Advertising API with Python. How can I do a keyword search for a book and get XML results that contain TITLE, ISBN, and PRICE for each entry?

Verbose version:

I am working in Python on a web site that allows the user to search for textbooks from different sites such as eBay and Amazon. Basically, I need to obtain simple information such as titles, ISBNS, and prices for each item from a set of search results from one of those sites. Then, I can store and format that information as needed in my application (e.g, displaying HTML).

In eBay’s case, getting the info I needed wasn’t too hard. I used urllib2 to make a request based on a sample I found. All I needed was a special security key to add to the URL:

def ebaySearch(keywords): #keywords is a list of strings, e.g. ['moby', 'dick'] 

  #findItemsAdvanced allows category filter -- 267 is books 
  #Of course, I replaced my security appname in the example below 

  url = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsAdvanced&SERVICE-NAME=FindingService&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=[MY-APPNAME]&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&categoryId=267&keywords="
 
  #Complete the url...  
  numKeywords = len(keywords)
  for k in range(0, numKeywords-1):
    url += keywords[k]
    url += "%20"  

  #There should not be %20 after last keyword
  url += keywords[numKeywords-1] 

  request = urllib2.Request(url) 
  response = urllib2.urlopen(request)  #file like thing (due to library conversion)

  xml_response = response.read()
  ... 

…Then I parsed this with minidom.

In Amazon’s case, it doesn’t seem to be so easy. I thought I would start out by just looking for an easy wrapper. But their developer site doesn’t seem to provide a python wrapper for what I am interested in (the Product Advertising API). One that I have tried, python-amazon-product-api 0.2.5 from https://pypi.python.org/pypi/python-amazon-product-api/, has been giving me some installation issues that may not be worth the time to look into (but maybe I’m just exasperated..). I also looked around and found pyaws and pyecs, but these seem to use deprecated authentication mechanisms.

I then figured I would just try to construct the URLs from scratch as I did for eBay. But Amazon requires a time stamp in the URLs, which I suppose I could programatically construct (perhaps something like these folks, who go the whole 9 yards with the signature: https://forums.aws.amazon.com/thread.jspa?threadID=10048).

Even if that worked (which I doubt will happen, given the amount of frustration the logistics have given so far), the bottom line is that I want name, price, and ISBN for the books that I search for. I was able to generate a sample URL with the tutorial on the API website, and then see the XML result, which indeed contained titles and ISBNs. But no prices! Gah! After some desperate Google searching, a slight modification to the URL (adding &ResponseGroup=Offers and &MerchantID=All) did the trick, but then there were no titles. (I guess yet another question I would have, then, is where can I find an index of the possible ResponseGroup parameters?)

Overall, as you can see, I really just don’t have a solid methodology for this. Is the construct-a-url approach a decent way to go, or will it be more trouble than it is worth? Perhaps the tl;dr at the top is a better representation of the overall question.

Asked By: norman

||

Answers:

If you have installation issues with python-amazon-product-api, send details to mailing list and you will be helped.

Answered By: Sebastian

Another way could be amazon-simple-product-api:

from amazon.api import AmazonAPI
amazon = AmazonAPI(ACCESS_KEY, SECRET, ASSOC)
results = amazon.search(Keywords = "book name", SearchIndex = "Books")
for item in results:
    print item.title, item.isbn, item.price_and_currency

To install, just clone from github and run

sudo python setup.py install

Hope this helps!

Answered By: cenna75