Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for books with the Amazon Product Advertising API - Python

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.

like image 303
norman Avatar asked May 01 '13 23:05

norman


People also ask

What is Amazon product Advertising API?

Product Advertising API operations open the doors to Amazon's databases so that you can take advantage of Amazon's sophisticated e-commerce data and functionality. Build your own web store to sell Amazon items or your own items.

How do I scrape data from Amazon search results in Python?

Running the Amazon Scraper to Scrape Search Result You can start your scraper by typing the command: python3 searchresults.py Once the scrape is complete you should see a file called search_results_output.jsonl with your data.

How to form pricing strategies for your Amazon products?

These data feeds can help you form pricing strategies by looking at your competition – other sellers or brands. Amazon provides a Product Advertising API, but like most other “API”s, this API doesn’t provide all the information that Amazon has on a product page. A web scraper can help you extract all the details displayed on the product page.

What's new in Product Advertising API 5?

On a high level, Product Advertising API 5.0 offers following new features: Ease of integration: Out of the box SDKs in popular languages for easy integration and adoption. The SDKs takes away all the heavy lifting of signing the request, request serialization and response de-serialization.


1 Answers

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!

like image 100
cenna75 Avatar answered Nov 15 '22 00:11

cenna75