获取python/Django中所有符号的实时数据

2024-09-30 01:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个股票预测项目。这就是我想要的:

要显示Nifty50、Nifty100左右的所有可用股票,然后用户将选择该股票,仅在第二天预测股票的高和低价格

我用的是Django

我到目前为止所做的: 我可以显示一个股票列表

def index(request):
    api_key = 'myAPI_Key'

    url50 = 'https://archives.nseindia.com/content/indices/ind_nifty50list.csv'
    url100 = 'https://archives.nseindia.com/content/indices/ind_nifty100list.csv'
    url200 = 'https://archives.nseindia.com/content/indices/ind_nifty200list.csv'

    sfifty = requests.get(url50).content
    shundred = requests.get(url100).content
    stwohundred = requests.get(url200).content

    nifty50 = pd.read_csv(io.StringIO(sfifty.decode('utf-8')))
    nifty100 = pd.read_csv(io.StringIO(shundred.decode('utf-8')))
    nifty200 = pd.read_csv(io.StringIO(stwohundred.decode('utf-8')))

    nifty50 = nifty50['Symbol']
    nifty100 = nifty100['Symbol']
    nifty200 = nifty200['Symbol']



    context = {
        'fifty': nifty50,
        'hundred': nifty100,
        'twohundred': nifty200
               }

    return render(request, 'StockPrediction/index.html', context)

我想要什么: 我想得到所有股票的实时数据openhighLTPChangeVolume。实时数据的意思是,它会随着股票价值的变化而变化

请帮忙


Tags: csvhttpscomgetcontentrequestspd股票
1条回答
网友
1楼 · 发布于 2024-09-30 01:19:29

您必须组合下面类似Ajax/Jquery的代码,定期获取DOM中的数据并更新值:

(function getStocks() {
    $.ajax({
            type: "GET",
            url: "url to your view",
            success: function (data) {
                // here you can get data from backend and do changes like
                // changing color by the data coming from your view.
            }
        }).then(function() {           // on completion, restart
       setTimeout(getStocks, 30000);  // function refers to itself
    });
})();

但是要小心发出过多的请求,您必须在这一行选择适当的间隔setTimeout(getStocks, "proper interval");

view中,您应该将查询转换为JSON格式,如下所示:

return JsonResponse({'stocks': stocks})

此处stocks必须为json格式

相关问题 更多 >

    热门问题