Amazon sales stats grabber

Thursday 8 September 2005

Let's say your wife has just published a book. Let us further suppose that you are a statistics-obsessed geek (not that there's anything wrong with that!) You're probably going to want a Python script that uses the Amazon web services to check the book's sale ranking. I just happen to have one:

# Amazon stats grabber
# Ned Batchelder,, September 2005.

# A list of Amazon ASIN's to get stats for:
asins = [
    '1590302443',   # Making Peace with Autism
    '1932565167',   # Thorn in My Pocket
    '0385504209',   # DaVinci Code, why not aim high? :-)

# Put your own Amazon Web Services subscription id here:
subid = '1AY..............S82'

# The Amazon response groups you want to retrieve for the items.
respgroups = 'SalesRank,ItemAttributes,Reviews'

# How you want the results formatted.
format = "%(SalesRank)7s [ %(TotalReviews)4s %(AverageRating)3s ]  %(Title)s"

# The rest of the file doesn't need to be customized.

import urllib
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
from xml import xpath

urlfmt = '' \
    'Service=AWSECommerceService&SubscriptionId=%(subid)s&' \
    'Operation=ItemLookup&ItemId=%(asin)s&' \

def load_stats(asins):
    """ Returns a dictionary of stats. """
    asin = ",".join(asins)
    slots = dict(globals())
    url = urlfmt % slots
    f = urllib.urlopen(url)
    content =
    alldata = PyExpat.Reader().fromString(content)
    # To get a look at the raw data, uncomment this:
    return alldata

def get_stats(alldata, asin):
    itemdata = xpath.Evaluate('.//Item[ASIN/text()="%s"]' % asin, alldata)[0]
    stats = {}
    for tag in ['SalesRank', 'Title', 'AverageRating', 'TotalReviews']:
        elts = xpath.Evaluate(".//%s/text()" % tag, itemdata)
        val = '---'
        if elts:
            val = elts[0].data
            if ':' in val:
                val = val.split(':')[0]
            val = val.strip()
        stats[tag] = val
    return stats

alldata = load_stats(asins)

for asin in asins:
    stats = get_stats(alldata, asin)
    print format % stats

When run, it will write a simple report to standard out.


jay 3:48 PM on 8 Sep 2005

Then all you have to do is integrate RRDTool (
and you can have a live dashboard with eye candy.

Stevievep 11:16 AM on 18 May 2007

That was clever. Re: 'you seem to have come from a search engine. Your search words (amazon sales stats) are highlighted on this page...however I already had that covered by visiting via googlecache. Nice, though. Kudos.

Add a comment:

Ignore this:
not displayed and no spam.
Leave this empty:
not searched.
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.