Shopify API JSON Feed / Limit 250 和 Pages / GET 每次都拉取完全相同的记录,而不是下一页

Shopify API JSON Feed / Limit 250 and Pages / GET pulls exactly the same records every time not next page

我正在尝试获取所有收集的列表,当我调用 API 来计算它们时:

https://[store-username].myshopify.com/admin/collects/count.json

HTTP/1.1 200 OK
{
  "count": 307
}

我知道限制是 250,页面默认是 1

https://[store-username].myshopify.com/admin/collects.json?limit=250&page=1

将获得 250 条记录。

当我执行第 2 页时 - 我将获得与第 1 页中完全相同的记录

https://[store-username].myshopify.com/admin/collects.json?limit=250&page=2

出于好奇,我尝试了第 10 页 - 这超出了范围 - 那将是 2500 > 307,它 returned 与第 1 页相同的 250

第二件事:

当我把它放在代码中时/python - 我编写脚本到 运行

https://[store-username].myshopify.com/admin/collects.json?limit=250&page=1

它得到 250 条记录,然后我做第 2 页

https://[store-username].myshopify.com/admin/collects.json?limit=250&page=2

和return是NONE

我正在扯头发,无法让所有 307 只更新我的数据库 250 - 所以我不知道为什么第 2 页

在浏览器中加载与第 1 页完全相同的记录,第 2 页加载 250 条记录,它应该是 307-250 = 57 条记录,在脚本中它正在拉 NONE。

你能帮忙吗?

def handle(self, *args, **options):
    security = urllib2.HTTPPasswordMgrWithDefaultRealm()
    security.add_password(None, "https://[store-username].myshopify.com/admin/collects/count.json",
                          “[credentials]”, "[credentials]")
    auth_handler = urllib2.HTTPBasicAuthHandler(security)
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)

    url = 'https://[store-username].myshopify.com/admin/collects/count.json'

    collect_feed = urllib2.urlopen(url)
    data = collect_feed.read()
    js = json.loads(str(data))
    count = int(js['count'])

    page_size = 250
    pages = int(math.ceil(count / page_size))

    list_of_collects = Collect.objects.all()
    if list_of_collects:
        list_of_collects.delete()

    current_page = 1

    while (current_page <= pages):
        opening_url = "https://[store-username].myshopify.com/admin/collects.json?limit=" + str(page_size) + '&page=' + str(current_page)
        security.add_password(None, opening_url,
                              "[credentials]", "[credentials]")
        auth_handler = urllib2.HTTPBasicAuthHandler(security)
        opener = urllib2.build_opener(auth_handler)
        urllib2.install_opener(opener)

        try:
            collect_feed = urllib2.urlopen(opening_url)
        except:
             collect_feed = None

        if collect_feed != None:
            data = collect_feed.read()
            try:
                js = json.loads(str(data))
            except:
                js = None

                for x in range(0, len(js['collects'])):
                    single_collect = list_of_collects.filter(collect_id=js['collects'][x]["id"]).first()
                    if single_collect == None:
                        # Create the model you want to save the image to
                        collect = Collect(collect_id=js['collects'][x]["id"],
                                  collection_id=js['collects'][x]["collection_id"],
                                  product_id=js['collects'][x]["product_id"])
                        collect.save()
                        print("Product and Collection connection number " + str(x) + " was successfully saved")
        print("NO FEED")
        print("BATCH IS SUCCESSFULLY SAVED, PROCESSING THE NEXT ONE")

        current_page += 1

    self.stdout.write(self.style.SUCCESS(‘Collects Updated.’))

当我运行我得到的脚本时:

Product and Collection connection number 0 was successfully saved
Product and Collection connection number 1 was successfully saved
Product and Collection connection number 2 was successfully saved
[…]
Product and Collection connection number 249 was successfully saved
FIRST BATCH IS SUCCESSFULLY SAVED, PROCESSING THE NEXT ONE
sleeping for 20 seconds
NO FEED
SECOND BATCH IS SUCCESSFULLY SAVED, PROCESSING THE NEXT ONE
Collects Updated.

根据代码第 2 页是 returning NONE,但为什么计数是 307 > 250...

您需要使用 & 来分隔查询字符串中的元素:

https://[store-username].myshopify.com/admin/collects.json?limit=250&page=1

请注意,您确实应该使用 Shopify python 客户端。

感谢 Daniel 的修复,这是最终脚本。

def handle(self, *args, **options):
    security = urllib2.HTTPPasswordMgrWithDefaultRealm()
    security.add_password(None, "https://[store-username].myshopify.com/admin/collects/count.json",
                          “[credentials]”, "[credentials]")
    auth_handler = urllib2.HTTPBasicAuthHandler(security)
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)

    url = 'https://[store-username].myshopify.com/admin/collects/count.json'

    collect_feed = urllib2.urlopen(url)
    data = collect_feed.read()
    js = json.loads(str(data))
    count = int(js['count'])
    print (count)

    page_size = 250
    pages = int(math.ceil(count / 250.0))

    list_of_collects = Collect.objects.all()
    if list_of_collects:
        list_of_collects.delete()

    current_page = 1

    while (current_page <= pages):
        opening_url = "https://[store-username].myshopify.com/admin/collects.json?limit=" + str(
            page_size) + '&page=' + str(current_page)
        security.add_password(None, opening_url,
                              "[credentials]", "[credentials]")
        auth_handler = urllib2.HTTPBasicAuthHandler(security)
        opener = urllib2.build_opener(auth_handler)
        urllib2.install_opener(opener)

        try:
            collect_feed = urllib2.urlopen(opening_url)
        except:
            collect_feed = None

        if collect_feed != None:
            data = collect_feed.read()

            try:
                js = json.loads(str(data))
            except:
                js = None

            if js != None:
                for x in range(0, len(js['collects'])):
                    single_collect = list_of_collects.filter(collect_id=js['collects'][x]["id"]).first()
                    if single_collect == None:
                        # Create the model you want to save the image to
                        collect = Collect(collect_id=js['collects'][x]["id"],
                                          collection_id=js['collects'][x]["collection_id"],
                                          product_id=js['collects'][x]["product_id"])
                        collect.save()
                        print("Product and Collection connection number " + str(x) + " was successfully saved")

                print(str(current_page) + "ST BATCH IS SUCCESSFULLY SAVED, PROCESSING THE NEXT ONE")
            else:
                print ("Feed is empty.")
        current_page += 1
    self.stdout.write(self.style.SUCCESS(‘Collects Updated.'))