Skip to main content
 首页 » 编程设计

python之使用 shopify python api 从 shopify 中查找具有特定日期范围的客户

2025年04月02日16虾米姐

如何从 shopify 导入具有特定日期范围的客户,即使用日期过滤器导入客户?

不带过滤器导入客户请引用以下代码:

shop_url = "https://%s:%s@%s.myshopify.com/admin/" % (self.api_key_shopify,  
                                                    self.password_shopify, self.name) 
 
shopify.ShopifyResource.set_site(shop_url) 
customer_list = shopify.Customer.find() 

请您参考如下方法:

As you can see from the documentation ,为了获取在特定范围内创建的客户,您需要随请求传递的参数是 created_at_min 和 created_at_max。除非您想手动输入时间戳,否则我们将需要 datetime来自 datetime 的对象模块。

from datetime import datetime 
time_format = "%Y-%m-%dT%H:%M:%S+00:00" 
min_date = datetime(year=2016, month=5, day=1).strftime(time_format) 
max_date = datetime(year=2016, month=6, day=1).strftime(time_format) 

我们在 strftime 方法中使用的格式化字符串将为我们提供 Shopify 文档所需格式的时间,并硬编码 UTC 时区。如果您想使用不同的时区,您可以在不同的时间偏移中进行硬编码,或者使用 pytz module .

现在,实际调用 API。使用 Shopify 资源的 find 方法时,您将属性名称/值对作为关键字参数传递,如下所示:

customer_list = shopify.Customer.find( 
    created_at_min = date1, 
    created_at_max = date2 
) 

瞧,这应该返回一个 Customer 资源列表,如果没有匹配项,则返回一个空列表。