bur*_*007 5 python amazon-product-api
可能重复:
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)
Run Code Online (Sandbox Code Playgroud)
错误信息:
AttributeError:'LxmlItemSearchPaginator'对象没有属性'Items'
lxml已正确安装!
哪里出错了?
Gag*_*ngh 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
Run Code Online (Sandbox Code Playgroud)