I'm trying to get a number of products, which are limited by 250 items per call and combine them in one dictionary. The result (per page) is response.json()
itemcount = getProductCount() // 658
print("total products: " + str(itemcount))
pages = math.ceil(itemcount/250) // 3
page =1
print("pages: "+str(pages))
allproducts = {'products': [{}]}
while page <= pages:
print("page: " +str(page))
products = getProducts(lang, page, 250)
allproducts.update(products)
# for p in products['products']:
# print(p['id'])
page += 1
print(allproducts)
the outcome is something like this.
So, I can see that my loop is working and the number of pages are correct.
the only thing that is not correct is that allproducts only holds the last 158 items.
total products: 658
pages: 3
page: 1
page: 2
page: 3
{'products': [{'id': 63252188, 'createdAt': '2018-01-11T15:20:16+01:00', 'updatedAt': '2018-04-25T16:35:44+02:00', 'isVisible': True, 'visibility': 'visible', 'hasMatrix': False, 'data01': '', 'data02': '', 'data03': '
etc..
When I print out all p['id'] 's in each loop, I can see that it prints all 658 product id's
What am I missing here? Or should I do it completely different?
答案 0 :(得分:1)
我认为代码的问题在于.update
方法保持替换 allproducts['products']
的值,而不是将现有值与其他产品列表连接。 / p>
代替
allproducts = {'products': [{}]}
while page <= pages:
print("page: " +str(page))
products = getProducts(lang, page, 250)
allproducts.update(products)
尝试
allproducts = {'products': []} # modified [{}] to []
while page <= pages:
print("page: " +str(page))
products = getProducts(lang, page, 250)
allproducts['products'].extend(products['products']) # extend instead of update