Result pagination¶
New in version 0.2.5.
Changed in version 0.2.6.
One of the main advantages of this wrapper is that it provides automatic pagination of results. Rather than having to make 10 calls to get all available pages, you can simply iterate over the paginator instance that some operations return.
>>> api = API(locale='de')
>>> results = api.item_search('Books',
... Publisher='Galileo Press', Sort='salesrank')
>>> results
<amazonproduct.processors._lxml.SearchPaginator object at 0x253af10>
The result is a SearchPaginator
instance, which can be queried and iterated over
>>> results.results
286
>>> results.pages
29
>>> for item in results:
... print item.ASIN
...
B004C04AOG
B00K1ZG9V8
B00SWJNV2K
1408855658
1408845644
1783705485
3551551677
...
New pages are loaded from Amazon (up to a maximum of 10 pages) as they are required.
If you don’t want to use pagination, you can disable this feature by passing paginate=False
.
>>> results = api.item_search('Books',
... Publisher='Galileo Press', Sort='salesrank',
... ItemPage=3, paginate=False)
>>> results
<Element {http://webservices.amazon.com/AWSECommerceService/2013-08-01}ItemSearchResponse at 0xb5a845cc>
>>> for item in results.Items.Item: # now we have a normal result page!
... print item.ASIN
...
B00PQ63SUC
B013STZW4S
B00VJPQ9EG
1783296038
1455524182
1608876861
3551551936
3551559015
3833230347
...
Note
Now we have a single result page
Paginator types¶
By default the items will be paginated over. However, there are other pagination methods available:
ITEMS_PAGINATOR
(default) - iterates over all items (default)RELATEDITEMS_PAGINATOR
- iterates over all related items providedFalse
- no pagination thank you very much
All paginator classes inherit from BaseResultPaginator
:
-
class
amazonproduct.processors.
BaseResultPaginator
(fun, *args, **kwargs)¶ Wrapper class for paginated results. This class will call the passed function iteratively until either the specified limit is reached or all result pages, which can be retrieved, are fetched.
Note
Amazon does put a rather restrictive limit on pagination. Don’t expect to be able to retrieve all result pages!
A result paginator has the following attributes:
pages
(same aslen(<paginator>)
)- Number of total pages. This may differ from the number of pages
actually iterated over because of limits either imposed by Amazon or
yourself (using
limit
). results
- Number of total results. This may differ from the number of results actually retrievable because Amazon generally limits pagination to ten pages.
current
- Number of result page retrieved last.