如何更新库存API中的价格不会更新

问题描述

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



enter image description here

解决方法

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...