股票实时交易数据之Python、Java等多种主流语言实例代码演示通过股票数据接口

复制代码
    如今,量化分析在股市领域风靡一时,股票数据用对比"突出"差异化。针对用户最关心的维度,用具体数据或场景对比,数据质量覆盖,易用性与支持。能帮读者快速锁定适配自身需求的接口。

我们聚焦最实用的场景,并通过Python、JavaScript(Node.js)、Java、C#、Ruby等五种主流语言,逐一演示如何高效获取各类股票数据,为大家投资决策提供更全面的参考。

先把数据接口的地址给大家,大家可以直接点击地址或复制到地址栏打开,马上就可以验证接口的有效性

沪深A股实时交易数据API接口:http://api.momaapi.com/hsstock/real/time/股票代码/证书您的Token

接口URL中,000001是股票代码,是请求证书,TEST-API-TOKEN-MOMA-836089C22111这个是官方提供的测试证书只能测试000001的数据

1、python

python 复制代码
import requests  
  
url = "http://api.momaapi.com/hsstock/real/time/000001/TEST-API-TOKEN-MOMA-836089C22111"  
response = requests.get(url)  
data = response.json()  
print(data)

2、JavaScript (Node.js)

javascript 复制代码
const axios = require('axios');  
  
const url = "http://api.momaapi.com/hsstock/real/time/000001/TEST-API-TOKEN-MOMA-836089C22111";  
axios.get(url)  
  .then(response => {  
    console.log(response.data);  
  })  
  .catch(error => {  
    console.log(error);  
  });

3、Java

java 复制代码
import java.net.URI;  
import java.net.http.HttpClient;  
import java.net.http.HttpRequest;  
import java.net.http.HttpResponse;  
import java.io.IOException;  
  
public class Main {  
    public static void main(String[] args) {  
        HttpClient client = HttpClient.newHttpClient();  
        HttpRequest request = HttpRequest.newBuilder()  
            .uri(URI.create("
                            http://api.momaapi.com/hsstock/real/time/000001/TEST-API-TOKEN-MOMA-836089C22111"))  
            .build();  
  
        try {  
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());  
            System.out.println(response.body());  
        } catch (IOException | InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}

4、C#

csharp 复制代码
using System;  
using System.Net.Http;  
using System.Threading.Tasks;  
  
class Program  
{  
    static async Task Main()  
    {  
        using (HttpClient client = new HttpClient())  
        {  
            string url = "http://api.momaapi.com/hsstock/real/time/000001/TEST-API-TOKEN-MOMA-836089C22111";  
            HttpResponseMessage response = await client.GetAsync(url);  
            string responseBody = await response.Content.ReadAsStringAsync();  
            Console.WriteLine(responseBody);  
        }  
    }  
}

5、Ruby

ruby 复制代码
require 'net/http'  
require 'json'  
  
url = URI("http://api.momaapi.com/hsstock/real/time/000001/TEST-API-TOKEN-MOMA-836089C22111")  
  
http = Net::HTTP.new(url.host, url.port)  
request = Net::HTTP::Get.new(url)  
response = http.request(request)  
data = JSON.parse(response.read_body)  
puts data

返回数据示例:

json 复制代码
  "pe": 4.61,
    "ud": -0.19,
    "pc": -1.586,
    "zf": 2.0033,
    "tr": 0.41,
    "pb_ratio": 0.52,
    "p": 11.79,
    "o": 11.98,
    "h": 12.01,
    "l": 11.77,
    "yc": 11.98,
    "cje": 1309217800,
    "v": 1103627,
    "pv": 110362708,
    "tv": 88,
    "t": "2025-09-03 14:12:51"
}

返回的数据字段说明:

字段名称 数据类型 字段说明
p number 最新价
o number 开盘价
h number 最高价
l number 最低价
yc number 前收盘价
cje number 成交总额
v number 成交总量
pv number 原始成交总量
ud float 涨跌额
pc float 涨跌幅
zf float 振幅
t string 更新时间
pe number 市盈率
tr number 换手率
pb_ratio number 市净率
tv number 成交量
相关推荐
weixin_523185321 分钟前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
点燃大海3 分钟前
SpringAI构建智能体
java·spring boot·spring·springai智能体
xier_ran4 分钟前
【infra之路】02_RadixAttention与KV_Cache管理
java·spring boot·spring
黑马师兄18 分钟前
RAG混合检索深度解析:让AI真正找到你要的内容
java·人工智能·ai·agent·rag·ai-native
码客日记22 分钟前
Spring Boot 配置文件敏感信息加密(Jasypt 企业级完整方案)
java·spring boot·git
宋拾壹37 分钟前
同时添加多个类目
android·开发语言·javascript
IT知识分享42 分钟前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
lunzi_08261 小时前
【学习笔记】《Python编程 从入门到实践》第8章:函数定义、参数传递与模块导入
笔记·python·学习
凡人叶枫1 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
杨运交1 小时前
[030][Web模块]Spring Boot 验证与 OpenAPI 集成实战:从校验规则到文档生成
前端·spring boot·python