如何更新股票中的价格 API 不更新

How to update the prices in the stock API doesn't update

我正在尝试使用 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']))
        



可以,但您需要在 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)