【发布时间】:2020-04-23 14:32:55
【问题描述】:
对于私有 Shopify 应用,我想检索所有客户数据并写入 csv 文件。我尝试了下面的选项来一次获取分页 250 条记录。但我收到一个错误: HTTPError: 错误请求
shopify.ShopifyResource.set_site(shop_url)
import sys
import pandas as pd
% Get all customers
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
all_customers = get_all_resources(shopify.Customer)
data=[]
for customer in all_customers:
tempdata=[]
tempdata.append(customer.id)
tempdata.append(customer.first_name)
tempdata.append(customer.last_name)
tempdata.append(customer.addresses)
tempdata.append(customer.phone)
tempdata.append(customer.email)
data.append(tempdata)
df=pd.DataFrame(data,columns=['CustomerCode','FirstName','LastName','Address','MobileNo','Email'])
df.to_csv('CustomerDataFromServer.csv',index=False)
shopify.ShopifyResource.clear_session()
【问题讨论】:
-
如果您更具体地了解哪一行引发了错误,这将有所帮助。我不熟悉shopify API,但想知道
resource_count = resource.count(**kwargs)在kwargs为空时是否有效?无论如何,kwargs在那个函数中有什么意义 - 它只被调用一次,带有一个 arg,所以kwargs总是空的。使用函数本地的字典可能会更好。