5

可能重复:
适用于 Python 的 Amazon API 库?

我想使用 python-amazon-product-api 和 lxml.objectify 对 Amazon 产品 API 进行简单查询。

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)

错误信息:

AttributeError:“LxmlItemSearchPaginator”对象没有属性“项目”

lxml 已正确安装!

错误在哪里?

4

1 回答 1

10

我也遇到了类似的问题,示例中的节点包含 LxmlItemSearchPaginator 而不是实际结果。这是完整的工作示例。

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
于 2011-10-16T08:55:07.563 回答