shopifyapi JSON Feed/Limit 250和Pages/GET每次都会提取完全相同的记录,而不是下一个pag

2024-09-28 22:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取所有收集的列表,当我调用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张唱片。在

当我在第二页做同样的记录时

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

第307页和第250页一样,我会返回第250页

第二件事:

当我把它放在code/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

而回报是没有的

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

在浏览器中加载与第1页完全相同的记录,第2页加载的记录应为307-250=57条记录,而在脚本中则不加载任何记录。在

你能帮忙吗?在

^{pr2}$

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

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返回的是NONE,但为什么count是307>;250。。。在


Tags: andstorehttpscomjsonadmin记录page
2条回答

这是最后的剧本多亏了丹尼尔的修复。在

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.'))

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

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

不过,请注意,您应该使用python客户端。在

相关问题 更多 >