如今,量化分析在股市领域风靡一时,其核心要素在于数据,获取股票数据,是踏上量化分析之路的第一步。你可以选择亲手编写爬虫来抓取,但更便捷的方式,莫过于利用专业的股票数据API接口。自编爬虫虽零成本,却伴随着时间与精力的巨大消耗,且常因目标页面变动而失效。大家可以依据自己的实际情况来决定数据获取方式。
接下来,我将分享200多个实测可用且免费的专业股票数据接口,并通过Python、JavaScript(Node.js)、Java、C#、Ruby等五种主流语言,逐一演示如何高效获取各类股票数据,希望能够对大家有所帮助。
先把数据接口的地址给大家,大家可以直接点击地址或复制到地址栏打开,马上就可以验证接口的有效性
沪深A股近一年各季度利润数据API接口:http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-9F96-0C7F0FBCD073
接口URL中,000001是股票代码,LICENCE-66D8-9F96-0C7F0FBCD073是请求证书,这个是官方提供的测试证书只能测试000001的数据,随后大家自己可以去领取一个免费的请求证书就可以获取其他股票的数据了。
1、python
importrequests url="http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-9F96-0C7F0FBCD073"response=requests.get(url)data=response.json()print(data)2、JavaScript (Node.js)
constaxios=require('axios');consturl="http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-9F96-0C7F0FBCD073";axios.get(url).then(response=>{console.log(response.data);}).catch(error=>{console.log(error);});3、Java
importjava.net.URI;importjava.net.http.HttpClient;importjava.net.http.HttpRequest;importjava.net.http.HttpResponse;importjava.io.IOException;publicclassMain{publicstaticvoidmain(String[]args){HttpClientclient=HttpClient.newHttpClient();HttpRequestrequest=HttpRequest.newBuilder().uri(URI.create("http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-9F96-0C7F0FBCD073")).build();try{HttpResponse<String>response=client.send(request,HttpResponse.BodyHandlers.ofString());System.out.println(response.body());}catch(IOException|InterruptedExceptione){e.printStackTrace();}}}4、C#
usingSystem;usingSystem.Net.Http;usingSystem.Threading.Tasks;classProgram{staticasyncTaskMain(){using(HttpClientclient=newHttpClient()){stringurl="http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-9F96-0C7F0FBCD073";HttpResponseMessageresponse=awaitclient.GetAsync(url);stringresponseBody=awaitresponse.Content.ReadAsStringAsync();Console.WriteLine(responseBody);}}}5、Ruby
require'net/http'require'json'url=URI("http://api.mairuiapi.com/hscp/jdlr/000001/LICENCE-66D8-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返回数据示例:
[{"date":"2025-03-31","income":"3,370,900.00","expend":"936,900.00","profit":"1,691,000.00","totalp":"1,688,300.00","reprofit":"1,409,600.00","basege":"0.6200","ettege":"0.6200","otherp":"-79,800.00","totalcp":"1,329,800.00"},{"date":"2024-12-31","income":"14,669,500.00","expend":"4,206,100.00","profit":"5,520,600.00","totalp":"5,473,800.00","reprofit":"4,450,800.00","basege":"2.1500","ettege":"2.1500","otherp":"-37,400.00","totalcp":"4,413,400.00"},{"date":"2024-09-30","income":"11,158,200.00","expend":"3,169,900.00","profit":"4,786,900.00","totalp":"4,774,400.00","reprofit":"3,972,900.00","basege":"1.9400","ettege":"1.9400","otherp":"-78,600.00","totalcp":"3,894,300.00"},{"date":"2024-06-30","income":"7,713,200.00","expend":"2,189,200.00","profit":"3,208,700.00","totalp":"3,197,700.00","reprofit":"2,587,900.00","basege":"1.2300","ettege":"1.2300","otherp":"-35,600.00","totalcp":"2,552,300.00"},{"date":"2024-03-31","income":"3,877,000.00","expend":"1,082,000.00","profit":"1,855,400.00","totalp":"1,852,500.00","reprofit":"1,493,200.00","basege":"0.6600","ettege":"0.6600","otherp":"34,500.00","totalcp":"1,527,700.00"}]返回的数据字段说明:
date代表:截止日期yyyy-MM-dd,income代表:营业收入(万元),expend代表:营业支出(万元),profit代表:营业利润(万元),totalp代表:利润总额(万元),reprofit代表:净利润(万元),basege代表:基本每股收益(元/股),ettege代表:稀释每股收益(元/股),otherp代表:其他综合收益(万元),totalcp代表:综合收益总额(万元)