Python 如何更新股票API中的价格';t更新

Python 如何更新股票API中的价格';t更新,python,list,loops,dictionary,stock,Python,List,Loops,Dictionary,Stock,我试图使用python从API中获取股票价格,但问题是,当我将其放入while循环中时,它不会更新,而价格在API中更新,另外,是否每5分钟进行一次循环?代码如下: 导入urllib.request 导入json URL价格=”https://financialmodelingprep.com/api/v3/quote-short/AMZN?apikey=555555555555555555" obj=urllib.request.urlopen(urlprices) data=json.lo

我试图使用python从API中获取股票价格,但问题是,当我将其放入while循环中时,它不会更新,而价格在API中更新,另外,是否每5分钟进行一次循环?代码如下:


导入urllib.request
导入json
URL价格=”https://financialmodelingprep.com/api/v3/quote-short/AMZN?apikey=555555555555555555"
obj=urllib.request.urlopen(urlprices)
data=json.load(obj)
a=0
当a==0时:
打印(浮动(数据[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)

这是可能的,但您需要在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)

为什么会改变?你的代码只调用一个API你从不更新数据-你只是一遍又一遍地打印同样的东西。所以这是不可能的?是的,我知道,这就是为什么我问你是否有可能在循环中睡眠5分钟,然后进行更新。为什么会改变?你的代码只调用一个API你从不更新数据-你只是一遍又一遍地打印同样的东西。所以这是不可能的?是的,我知道,这就是为什么我问你是否有可能在循环中睡5分钟,然后再进行更新。你们真是太棒了!它成功了,多亏了你们两个,我会在3分钟内把答案标记为正确的。你们真是太棒了!它成功了,多亏了你们两位,我会在3分钟内把答案标记为正确