我想获得25000名客户的订单历史记录。我有每个客户的电子邮件地址。我目前的流程是循环浏览电子邮件地址,获取相应的客户,然后获取对应于该客户的订单。
我想通过以下一项或两项来加快速度:
1) 分批从电子邮件中获取客户,而不是一次一封
2) 直接从客户电子邮件转到订单列表。
如何实现这两个目标之一?有没有更好的方法来实现我忽视的目标?
编辑:我的当前(正在工作但速度较慢)代码:
import json
from woocommerce import API
with open("credentials/woocommerce_api_keys.json",\'r\') as f:
woo_credentials = json.load(f)
wcapi = API(
url="https://monq.com",
consumer_key=woo_credentials["consumer_key"],
consumer_secret=woo_credentials["consumer_secret"],
version="wc/v3"
)
with open("customer_emails.csv",\'r\') as f:
customer_emails = f.read().split(",")
customers = []
orders_by_customer = {}
order_dates_by_customer = {}
for email in customer_emails:
customer = wcapi.get("customers/?email={}".format(email)).json()
if len(customer) == 1:
customer = customer[0]
customers.append(customer)
customer_id = customer["id"]
orders = wcapi.get("orders/?customer={}".format(customer_id)).json()
orders_by_customer[email] = orders
order_dates = []
for o in orders:
order_dates.append(o["date_modified_gmt"])
order_dates_by_customer[email] = order_dates
else:
print("email {} returns {} customers".format(email,len(customer)))
with open("data/customers.json",\'w\') as f:
json.dump(customers,f,indent=4)
with open("data/orders_by_customer.json",\'w\') as f:
json.dump(orders_by_customer,f,indent=4)
with open("data/order_dates_by_customer.json","w") as f:
json.dump(order_dates_by_customer,f,indent=4)