自学内容网 自学内容网

【股票数据API接口02】如何获取股票历史交易数据之Python、Java等多种主流语言实例代码演示通过股票数据接口获取数据

​ 如今,量化分析在股市领域风靡一时,其核心要素在于数据,获取股票数据,是踏上量化分析之路的第一步。你可以选择亲手编写爬虫来抓取,但更便捷的方式,莫过于利用专业的股票数据API接口。自编爬虫虽零成本,却伴随着时间与精力的巨大消耗,且常因目标页面变动而失效。大家可以依据自己的实际情况来决定数据获取方式。

接下来,我将分享200多个实测可用且免费的专业股票数据接口,并通过Python、JavaScript(Node.js)、Java、C#、Ruby等五种主流语言,逐一演示如何高效获取各类股票数据,希望能够对大家有所帮助。

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

沪深A股最新分时交易数据API接口:http://api.mairui.club/hszbl/fsjy/000001/60m/LICENCE-66D8-9F96-0C7F0FBCD073

​ 接口URL中,000001是股票代码,60m是分时级别,LICENCE-66D8-9F96-0C7F0FBCD073是请求证书,这个是官方提供的测试证书只能测试000001的数据,随后大家自己可以去领取一个免费的请求证书就可以获取其他股票的数据了。

​ 分时级别: 5m(5分钟)、15m(15分钟)、30m(30分钟)、60m(60分钟)、dn(日线未复权)、dq(日线前复权)、dh(日线后复权)、wn(周线未复权)、wq(周线前复权)、wh(周线后复权)、mn(月线未复权)、mq(月线前复权)、mh(月线后复权)、yn(年线未复权)、yq(年线前复权)、yh(年线后复权)

1、python

import requests  
  
url = "http://api.mairui.club/hszbl/fsjy/000001/60m/LICENCE-66D8-9F96-0C7F0FBCD073"  
response = requests.get(url)  
data = response.json()  
print(data)

2、JavaScript (Node.js)

const axios = require('axios');  
  
const url = "http://api.mairui.club/hszbl/fsjy/000001/60m/LICENCE-43D5-9F96-0C7F0FBCD073";  
axios.get(url)  
  .then(response => {  
    console.log(response.data);  
  })  
  .catch(error => {  
    console.log(error);  
  });

3、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.mairui.club/hszbl/fsjy/000001/60m/LICENCE-43D5-9F96-0C7F0FBCD073"))  
            .build();  
  
        try {  
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());  
            System.out.println(response.body());  
        } catch (IOException | InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}

4、C#

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.mairui.club/hszbl/fsjy/000001/60m/LICENCE-43D5-9F96-0C7F0FBCD073";  
            HttpResponseMessage response = await client.GetAsync(url);  
            string responseBody = await response.Content.ReadAsStringAsync();  
            Console.WriteLine(responseBody);  
        }  
    }  
}

5、Ruby

require 'net/http'  
require 'json'  
  
url = URI("http://api.mairui.club/hszbl/fsjy/000001/60m/LICENCE-43D5-9F96-0C7F0FBCD073")  
  
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

返回数据示例:

[{"d":"2024-08-27 15:00","o":"10.43","h":"10.45","l":"10.42","c":"10.43","v":"163804","e":"170990736.00","zf":"0.29","hs":"0.08","zd":"0.00","zde":"0.00"},{"d":"2024-08-28 10:30","o":"10.41","h":"10.43","l":"10.28","c":"10.31","v":"382111","e":"395503852.00","zf":"1.44","hs":"0.20","zd":"-1.15","zde":"-0.12"},{"d":"2024-08-28 11:30","o":"10.32","h":"10.38","l":"10.29","c":"10.36","v":"184046","e":"190148007.00","zf":"0.87","hs":"0.09","zd":"0.48","zde":"0.05"},{"d":"2024-08-28 14:00","o":"10.36","h":"10.36","l":"10.29","c":"10.31","v":"177854","e":"183509437.00","zf":"0.68","hs":"0.09","zd":"-0.48","zde":"-0.05"},{"d":"2024-08-28 15:00","o":"10.31","h":"10.33","l":"10.29","c":"10.33","v":"198536","e":"204710849.00","zf":"0.39","hs":"0.10","zd":"0.19","zde":"0.02"},{"d":"2024-08-29 10:30","o":"10.31","h":"10.38","l":"10.18","c":"10.24","v":"452429","e":"464700149.00","zf":"1.94","hs":"0.23","zd":"-0.87","zde":"-0.09"},{"d":"2024-08-29 11:30","o":"10.24","h":"10.24","l":"10.14","c":"10.17","v":"288148","e":"293223054.00","zf":"0.98","hs":"0.15","zd":"-0.68","zde":"-0.07"},{"d":"2024-08-29 14:00","o":"10.17","h":"10.18","l":"10.11","c":"10.14","v":"210424","e":"213224808.00","zf":"0.69","hs":"0.11","zd":"-0.29","zde":"-0.03"},{"d":"2024-08-29 15:00","o":"10.13","h":"10.16","l":"10.12","c":"10.13","v":"246676","e":"250203075.00","zf":"0.39","hs":"0.13","zd":"-0.10","zde":"-0.01"},{"d":"2024-08-30 10:30","o":"10.11","h":"10.24","l":"10.11","c":"10.13","v":"481791","e":"490082225.00","zf":"1.28","hs":"0.25","zd":"0.00","zde":"0.00"},{"d":"2024-08-30 11:30","o":"10.14","h":"10.26","l":"10.11","c":"10.25","v":"337418","e":"344039027.00","zf":"1.48","hs":"0.17","zd":"1.18","zde":"0.12"},{"d":"2024-08-30 14:00","o":"10.24","h":"10.26","l":"10.20","c":"10.21","v":"245258","e":"250881200.00","zf":"0.59","hs":"0.13","zd":"-0.39","zde":"-0.04"},{"d":"2024-08-30 15:00","o":"10.21","h":"10.22","l":"10.16","c":"10.16","v":"229592","e":"233856223.00","zf":"0.59","hs":"0.12","zd":"-0.49","zde":"-0.05"}]

返回的数据字段说明:

d代表:交易时间,短分时级别格式为YYYY-MM-DD HH:MM,日线级别为yyyy-MM-dd,o代表:开盘价(元),h代表:最高价(元),l代表:最低价(元),c代表:收盘价(元),v代表:成交量(手),e代表:成交额(元),zf代表:振幅(%),hs代表:换手率(%),zd代表:涨跌幅(%),zde代表:涨跌额(元),


原文地址:https://blog.csdn.net/Eumenides_max/article/details/145058385

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!