Sebastian Rahlf  committed a767ddf

Added hint for understanding XML responses (closes #35).

  • Participants
  • Parent commits f4315b4
  • Branches default

Comments (2)

  1. timesshidai

    Hi, Sebastian,

    I didn't quite understand where should I modify? I have the error:AttributeError: no such child: {}Items

    Here is my code.

    from amazonproduct import API
    AWS_KEY = '...'
    SECRET_KEY = '...'
    ASSOC_TAG  = '...'
    if __name__ == '__main__':
        api = API(AWS_KEY, SECRET_KEY, 'us', ASSOC_TAG)
        for root in api.item_search('Books', Publisher='Apress',
            # extract paging information
            total_results = root.Items.TotalResults.pyval
            total_pages = root.Items.TotalPages.pyval
                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

    Thanks for your help.

    I am looking forward to your reply.



  2. Sebastian Rahlf author

    What you have to bare in mind is that item_search() returns a paginator object which can be iterated. That means

    results = api.item_search('Books', Publisher='Apress', ResponseGroup='Large')

    will get you the paginator which contains attributes current (for current page), pages (no of pages) and results (no of results).

    Iterating this paginator

    from lxml import etree
    for item in api.item_search('Books', Publisher='Apress', ResponseGroup='Large'):
            print etree.tostring(item, pretty_print=True)

    will give you the individual items. Try using print etree.tostring(item, pretty_print=True) to see the structure of the XML objects being returned.

Files changed (1)

File docs/source/faq.rst

 Here is a growing collection of questions that pop up regularly.
+I read the API docs but I can't manage to get this to work
+The XML structure returned by Amazon is sometimes not easy to understand. Try
+the following::
+    from lxml import etree
+    api = API(locale='...')
+    results ='...') # your API call
+    print etree.tostring(results, pretty_print=True)
+It will print the XML response nicely formatted.
 Which locale should I use and why is this important?