使用while true循环每30秒获取一次API数据期间的重复输出



我需要每隔30秒从API获取数据,并将它们与之前的值进行比较。其中一种方法是使用"While true"循环。下面是我的代码的简要说明:

import requests
import json
import datetime
List1 = []
List2 = []
while True :
Url = ' the URL of the API'
request1 = requests.request("GET", URL)
#get the data and append them to List1
List1.append(data1)
time.sleep(30)
request2 = requests.request("GET", URL)
#get the data and append them to List2
List2.append(data2)

#eventually, some logics using List1 and List2:
blah blah blah...............

当我使用上面的代码时,每次随机都会得到一些重复的输出。然而,如果我们删除&;while true&;,代码将运行一次,我们将不会有重复的输出。

(让我解释更多:例如,如果一个代码运行了5次,输出的名称是1,2,3,4和5;当我运行上面的代码(while为true)时,我将得到这些输出:1,2,1,4,2)

为什么会这样??

问题是我把List1List2出了'while true'循环!

最新更新