【问题标题】:How to get all product id from Shopify Python API如何从 Shopify Python API 获取所有产品 ID
【发布时间】:2017-09-20 09:37:49
【问题描述】:

我创建了一个私人 shopify 应用程序。它可以获取几乎所有带有产品ID的信息。但是我需要一个选项来获取具有 API 的商店中所有产品的产品 ID。我已经尝试了以下选项

shopify.Product.find()

但它只显示前 50 个产品。但是我的商店有超过 2.4k 的产品。

【问题讨论】:

    标签: python-2.7 api shopify


    【解决方案1】:

    自 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 个产品,这可能需要一些时间!

    文档: https://help.shopify.com/api/reference/product

    【讨论】:

    • 非常感谢您的解决方案。虽然我可以在发布问题后解决它。但我喜欢你的解决方案。 :)
    • 感谢 Rafayet,也许这会帮助其他人 :)
    • 谢谢朱利安。这非常有帮助。知道如何将它与 shopify.Order 一起使用吗?使用该函数时,只返回33项。
    • 请忽略。我想到了。使用shopify.Orders.find(status="any",limit="100")
    • 感谢您的解决方案。但是,在这种情况下无法正常工作:shopify.CustomerSavedSearch.find({customer_saved_search_id}).customers()
    【解决方案2】:

    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 你有错误的解决方案吗?
    【解决方案3】:

    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
    

    【讨论】:

    • 请原谅我的业余问题,但是您传递给此函数(资源)的参数是什么,是 api 调用的结果还是?因为我相信这个 sn-p 是我需要的,但我无法让它与我的电话一起工作,抱歉这个愚蠢的问题。
    • 没有愚蠢的问题 :-) 你需要传递你想要返回的资源。因此,如果您想获得所有产品,那就是“shopify.Product”。但它同样适用于 shopify.Customer、shopify.Address、shopify.Cart 等,这就是它被参数化的原因。希望这会有所帮助。
    【解决方案4】:

    认为这也可能会有所帮助:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多