我正在使用 Shopify API 获取订单数据(订单名称、UTM 参数等),但问题是 API 默认限制为 250 个订单。我知道响应头包含一个参数链接,其中包含 page_info,可用于分页和迭代以获取所有订单,但问题是即使使用所有这些,我也无法获取所有订单的详细信息(由于某种原因,很少有订单被跳过。不确定它是否只获取已履行的订单,或者是否有任何此类默认过滤器)。分页的另一个问题是分页时只能将 page_info 和 rel 作为唯一的参数。最终目标是获得所有订单(无论订单后来发生了什么)
这是我使用过的代码
orders = []
#Here, I'm defining the date for which I want to get the orders for
params = {'created_at_min': '2023-05-11T00:00:00%2B05:30', 'created_at_max': '2023-05-11T23:59:59%2B05:30', 'limit': '250'}
response = requests.get(url, headers=headers,params=params)
for item in response.json()['orders']:
orders.append(item['name'])
print(len(orders))
for i in range(0,100):
try:
#obtaining the page_info(we'll get this only if there are more than 250 orders)
next_page = response.headers['Link']
#print(next_page)
start = "page_info="
end = ">"
#just some string extraction to get the page_info part, you can ignore this
start_escaped = re.escape(start)
end_escaped = re.escape(end)
result = re.search(f"(?<={start_escaped}).*(?={end_escaped})", next_page).group()
print(result)
params = {'page_info':result,'rel':'next','limit':'250'}
print(params)
response = requests.get(url, headers=headers,params=params)
print(len(response.json()['orders']))
if len(response.json()['orders'])<250:
for item in response.json()['orders']:
orders.append(item['name'])
break
except:
print('failed')
break
orders= pd.DataFrame(orders,columns=["Order ID"])
在查询参数中,您需要传递
'status': 'any'
(https://shopify.dev/docs/api/admin-rest/2023-10/resources/order#get-orders?status=any) 一些订单是在 API GET 请求的默认设置中跳过