在嵌套的列表数据内循环迭代


id_ucprice = [[47348, 22900], [48340, 14700], [4834934, 14700], [348348, 15300]]
for ids in id_ucprice:
for x in range(ids[1]):
post_data = {
'price': ids[1],
'overpriced_email_price_change': False,
'apply_to_all': False,
}
for y in range(ids[0]):
response = requests.post(f'apiurl/{ids[0]}/pricing', cookies=cookies, headers=headers, json=post_data)
update_data = json.loads(response.text)
response_price = update_data['priceCents']
r_item = update_data['name']
r_id = update_data['id']
if response_price == ids[1]:
print(style.MAGENTA + f'[{datetime.now()}] => Updated Price: {response_price/100:.2f} | ID = > {r_id} | Item => {r_item}')

我希望每个[0][1]在各自的请求URL和jsondata中使用,但我在遍历每个列表时遇到麻烦。当我运行时,它只使用第一个,[47348, 22900],并继续使用它,而不遍历列表的其余部分

我认为你不需要嵌套的for循环。否则,在当前代码中,它将调用requests.post(f'apiurl/47348/pricing', cookies=cookies, headers=headers, json=post_data)47348次。

在外部for循环for ids in id_ucprice:中,我们得到ids[47348, 22900],等等。我们可以直接使用ids[0]ids[1]生成URL并调用API。

id_ucprice = [[47348, 22900], [48340, 14700], [4834934, 14700], [348348, 15300]]
for ids in id_ucprice:
post_data = {
'price': ids[1],
'overpriced_email_price_change': False,
'apply_to_all': False,
}
response = requests.post(f'apiurl/{ids[0]}/pricing', cookies=cookies, headers=headers, json=post_data)
update_data = json.loads(response.text)
response_price = update_data['priceCents']
r_item = update_data['name']
r_id = update_data['id']
if response_price == ids[1]:
print(style.MAGENTA + f'[{datetime.now()}] => Updated Price: {response_price/100:.2f} | ID = > {r_id} | Item => {r_item}')

最新更新