· 7 years ago · Aug 22, 2018, 04:24 AM
1Amazon Product API in Python [closed]
2from amazonproduct import API
3
4AWS_KEY = '..'
5SECRET_KEY = '..'
6
7api = API(AWS_KEY, SECRET_KEY, 'de')
8node = api.item_search('Books', Publisher='Galileo Press')
9
10# node object returned is a lxml.objectified element
11# .pyval will convert the node content into int here
12total_results = node.Items.TotalResults.pyval # <--- error
13total_pages = node.Items.TotalPages.pyval
14
15# get all books from result set and print author and title
16for book in node.Items.Item:
17 print '%s: "%s"' % (book.ItemAttributes.Author, book.ItemAttributes.Title)
18
19from amazonproduct import API
20
21AWS_KEY = '...'
22SECRET_KEY = '...'
23
24if __name__ == '__main__':
25
26 api = API(AWS_KEY, SECRET_KEY, 'us')
27
28 for root in api.item_search('Books', Publisher='Apress',
29 ResponseGroup='Large'):
30
31 # extract paging information
32 total_results = root.Items.TotalResults.pyval
33 total_pages = root.Items.TotalPages.pyval
34 try:
35 current_page = root.Items.Request.ItemSearchRequest.ItemPage.pyval
36 except AttributeError:
37 current_page = 1
38
39 print 'page %d of %d' % (current_page, total_pages)
40
41 #~ from lxml import etree
42 #~ print etree.tostring(root, pretty_print=True)
43
44 nspace = root.nsmap.get(None, '')
45 books = root.xpath('//aws:Items/aws:Item',
46 namespaces={'aws' : nspace})
47
48 for book in books:
49 print book.ASIN,
50 if hasattr(book.ItemAttributes, 'Author'):
51 print unicode(book.ItemAttributes.Author), ':',
52 print unicode(book.ItemAttributes.Title),
53 if hasattr(book.ItemAttributes, 'ListPrice'):
54 print unicode(book.ItemAttributes.ListPrice.FormattedPrice)
55 elif hasattr(book.OfferSummary, 'LowestUsedPrice'):
56 print u'(used from %s)' % book.OfferSummary.LowestUsedPrice.FormattedPrice