【发布时间】:2017-09-20 09:37:49
【问题描述】:
我创建了一个私人 shopify 应用程序。它可以获取几乎所有带有产品ID的信息。但是我需要一个选项来获取具有 API 的商店中所有产品的产品 ID。我已经尝试了以下选项
shopify.Product.find()
但它只显示前 50 个产品。但是我的商店有超过 2.4k 的产品。
【问题讨论】:
标签: python-2.7 api shopify
我创建了一个私人 shopify 应用程序。它可以获取几乎所有带有产品ID的信息。但是我需要一个选项来获取具有 API 的商店中所有产品的产品 ID。我已经尝试了以下选项
shopify.Product.find()
但它只显示前 50 个产品。但是我的商店有超过 2.4k 的产品。
【问题讨论】:
标签: python-2.7 api shopify
自 2019 年 7 月起更新:这将不再有效,因为它已被弃用并随后从 Shopify API 中删除。
更换详情见this answer。
原答案如下
Shopify 返回资源列表的分页响应。默认每页资源数为50,默认页面为1。因此,您的请求等同于以下内容:
shopify.Product.find(limit=50, page=1)
Shopify 允许您将每页的限制增加到 250。这是我用来获取所有给定资源的辅助函数:
def get_all_resources(resource, **kwargs):
resource_count = resource.count(**kwargs)
resources = []
if resource_count > 0:
for page in range(1, ((resource_count-1) // 250) + 2):
kwargs.update({"limit" : 250, "page" : page})
resources.extend(resource.find(**kwargs))
return resources
你可以这样使用它:
products = get_all_resources(shopify.Product)
你甚至可以传入参数。您的问题专门询问产品 ID - 如果您将查询限制为仅返回 ID,这将快得多(因为它不必引入任何产品变体):
product_ids = get_all_resources(shopify.Product, fields="id")
请注意,如果您有 2.4k 个产品,这可能需要一些时间!
【讨论】:
shopify.Orders.find(status="any",limit="100")
shopify.CustomerSavedSearch.find({customer_saved_search_id}).customers()
Shopify API 中的分页界面发生了变化,旧的“限制+页面”分页方式是removed in api version 2019-07。
换句话说:@Julien 接受的答案在此 api 版本及更高版本中不起作用。
我在这里使用relative cursor based pagination 的新方法重新创建了已接受答案的功能:
def get_all_resources(resource_type, **kwargs):
resource_count = resource_type.count(**kwargs)
resources = []
if resource_count > 0:
page=resource_type.find(**kwargs)
resources.extend(page)
while page.has_next_page():
page = page.next_page()
resources.extend(page)
return resources
【讨论】:
TypeError: expected string or bytes-like object 你有错误的解决方案吗?
Lennart Rolland 响应的扩展。
正如他所说,首选答案不再适用于 api 版本 2019-07。
我无法让他的代码示例工作,因为“列表没有函数 has_next_page()”的错误。
所以我写了一个使用“链接”标题 rel='next' 分页的示例。
def get_all_resources(resource):
page_info = str()
resources = list()
while True:
resources.extend(resource.find(limit=250, page_info=page_info))
cursor = shopify.ShopifyResource.connection.response.headers.get('Link')
if 'next' in cursor:
page_info = cursor.split(';')[-2].strip('<>').split('page_info=')[1]
else:
break
return resources
【讨论】:
认为这也可能会有所帮助:
def get_products():
"""
Returns a list of all products in form of response JSON
from Shopify API endpoint connected to storefront.
* Note: Shopify API allows 250 pruducts per call.
:return:
product_list (list): List containing all product response
JSONs from Shopify API.
"""
products = []
is_remaining = True
i = 1
while is_remaining:
if i == 1:
params = {
"limit": 250,
"page": i
}
response = requests.get(
"{}/products.json".format(SHOPIFY_ENDPOINT),
params=params
)
products.append(response.json()['products'])
i += 1
elif len(products[i-2]) % 250 == 0:
params = {
"limit": 250,
"page": i
}
response = requests.get(
"{}/products.json".format(SHOPIFY_ENDPOINT),
params=params
)
products.append(response.json()['products'])
i += 1
else:
is_remaining = False
products = [products[i][j]
for i in range(0, len(products))
for j in range(0, len(products[i]))
]
return products
【讨论】: