我试图使用python从api中获取股票价格,但问题是,当我将其放入一个while循环中时,它不会更新,而API中的价格正在更新,另外,是否每隔5分钟就有一个循环呢?下面是代码:
import urllib.request
import json
urlprices = "https://financialmodelingprep.com/api/v3/quote-short/AMZN?apikey=555555555555555555"
obj = urllib.request.urlopen(urlprices)
data = json.load(obj)
a = 0
while a == 0:
print(float(data[0]['price']))

发布于 2020-08-11 17:40:05
这是可能的,但您需要在while循环中更新数据:
import urllib.request
import json
import time
a = 0
while a == 0:
urlprices = "https://financialmodelingprep.com/api/v3/quote-short/AMZN?apikey=555555555555555555"
obj = urllib.request.urlopen(urlprices)
data = json.load(obj)
print(float(data[0]['price']))
# here you should add a pause so that the loop will not hit the request limit for the api
time.sleep(300)https://stackoverflow.com/questions/63363500
复制相似问题