您可能已经知道这一点,但我最好的猜测是您实际上并没有 5 页的文章。当您指定的页面大于最后一页时,您只需返回最后一页,这会导致文章重复。
最好的办法是先获取文章的计数,然后用它来确定总页数,并在 xrange 函数中使用它。这是一个函数:
def get_all_articles():
article_count = shopify.Article.count()
articles = []
if article_count > 0:
for page in xrange(1, ((article_count-1) // 50) + 2):
articles.extend( shopify.Article.find(page=page) )
return articles
您可以更进一步,将其通用化以处理任何可数的 shopify 资源:
def get_all_of_resource(resource, **kwargs):
resource_count = resource.count(**kwargs)
resources = []
if resource_count > 0:
for page in xrange(1, ((resource_count-1) // 250) + 2):
arguments = kwargs
arguments.update({"limit" : 250, "page" : page})
resources.extend( resource.find(**arguments) )
return resources
你会这样使用它:
shirts = get_all_of_resource(shopify.Product, product_type="T-Shirt")
如您所见,我还添加了传递其他参数以过滤结果的功能,并且还请求每页的最大项目数 (250)。