Amazon Product API in Python

Possible Duplicate:
Amazon API Library for Python?

I want to make a simple request to the Amazon API using python-amazon-product-api and lxml.objectify.

from amazonproduct import API

AWS_KEY = '..'
SECRET_KEY = '..'

api = API(AWS_KEY, SECRET_KEY, 'de')
node = api.item_search('Books', Publisher='Galileo Press')

# node object returned is a lxml.objectified element
# .pyval will convert the node content into int here
total_results = node.Items.TotalResults.pyval # <--- error
total_pages = node.Items.TotalPages.pyval

# get all books from result set and print author and title
for book in node.Items.Item:
    print '%s: "%s"' % (book.ItemAttributes.Author, book.ItemAttributes.Title)

Error message:

AttributeError: the object 'LxmlItemSearchPaginator' does not have the attribute 'Items'

lxml is installed correctly!

Where is the mistake?

+5
source share
1 answer

I also had a similar problem, the node in the example contains LxmlItemSearchPaginator instead of the actual result. Here is a complete working example.

from amazonproduct import API

AWS_KEY = '...'
SECRET_KEY = '...'

if __name__ == '__main__':

    api = API(AWS_KEY, SECRET_KEY, 'us')

    for root in api.item_search('Books', Publisher='Apress',
                                ResponseGroup='Large'):

        # extract paging information
        total_results = root.Items.TotalResults.pyval
        total_pages = root.Items.TotalPages.pyval
        try:
            current_page = root.Items.Request.ItemSearchRequest.ItemPage.pyval
        except AttributeError:
            current_page = 1

        print 'page %d of %d' % (current_page, total_pages)

        #~ from lxml import etree
        #~ print etree.tostring(root, pretty_print=True)

        nspace = root.nsmap.get(None, '')
        books = root.xpath('//aws:Items/aws:Item', 
                             namespaces={'aws' : nspace})

        for book in books:
            print book.ASIN,
            if hasattr(book.ItemAttributes, 'Author'): 
                print unicode(book.ItemAttributes.Author), ':', 
            print unicode(book.ItemAttributes.Title),
            if hasattr(book.ItemAttributes, 'ListPrice'): 
                print unicode(book.ItemAttributes.ListPrice.FormattedPrice)
            elif hasattr(book.OfferSummary, 'LowestUsedPrice'):
                print u'(used from %s)' % book.OfferSummary.LowestUsedPrice.FormattedPrice
+10
source

All Articles