不限语言·极速调取 | JQData HTTP 接口正式上线

部署运行你感兴趣的模型镜像

JQData HTTP接口

在新上线的 HTTP 接口中,我们采用 post 机制,用户只需提交 JSON body 参数,就能获取股票、指数、基金、期货、期权、宏观经济、因子数据、舆情数据等量化研究所需的各类金融数据。点击申请JQData使用权限。 下面我们来一一讲解JQData HTTP 接口的各个部分。

url
https://dataapi.joinquant.com/apis 请求JQData数据的http接口地址
token
请求数据接口前需要使用get_token函数获取调用数据的身份凭证,通过post方法传入申请JQData时填入的手机号和密码(密码是聚宽官网登录密码;新申请用户是手机号后6位):

body= {
"method" : "get_token",
 "mob" : "135xxxxxxx",
 "pwd" : "xxxxxxxxxx"
}

返回token结果如下所示:

body:通过post方法传入 JSON 格式的字符串,获取指定的金融量化数据,body 参数由如下部分组成:
```body={
"method" : 指定获取数据的方法名,调用指定数据品种
"token" : 调用数据接口的身份验证,当天有效,失效后可重新获取
"param1" : 指定method方法输入的参数1
"param2" : 指定method方法输入的参数2
…
}

输出结果:返回 CSV 格式的文本数据,例如使用 get_all_securities 方法获取所有标的信息的返回结果如下:

000001.XSHE,平安银行,PAYH,1991-04-03,2200-01-01,stock
000002.XSHE,万科A,WKA,1991-01-29,2200-01-01,stock
…

下面,我们来用具体的代码示例演示一下:

Python调用JQData HTTP示例

import requests, json
import pandas as pd

#获取调用凭证
url = "https://dataapi.joinquant.com/apis"
body = {
    "method": "get_token",
    "mob": "ID",  #ID是申请JQData时所填写的手机号
    "pwd": "Password",  # Password为聚宽官网登录密码,新申请用户默认为手机号后6位
}
response = requests.post(url, data=json.dumps(body))
token = response.text

#调用get_all_securities函数获取所有股票信息
body = {
    "method": "get_all_securities",
    "token": token,
    "code": "stock",
    "date": "2019-01-15"
}
response = requests.post(url, data=json.dumps(body))
stock = response.text

#写入CSV文件
with open('C:/Users/jase8/Desktop/test/stock.csv', 'w')as f:
    f.write(stock)

#读取CSV文件转化成dataframe
df = pd.read_csv('C:/Users/jase8/Desktop/test/stock.csv',
'utf-8',engine='python')
print(df)

        code,display_name,name,start_date,end_date,type
0     000001.XSHE,平安银行,PAYH,1991-0 4-03,2200-01-01,stock
1       000002.XSHE,万科A,WKA,1991-01-29,2200-01-01,stock
2     000004.XSHE,国农科技,GNKJ,1990-12-01,2200-01-01,stock
3     000005.XSHE,世纪星源,SJXY,1990-12-10,2200-01-01,stock

Matlab调用JQData HTTP示例

slCharacterEncoding('UTF-8')
url = 'https://dataapi.joinquant.com/apis';
options = weboptions('RequestMethod','post','MediaType','application/json');
body = struct('method','get_token','mob','135xxxxxxx','pwd','xxxxxxxxxx');
token = webwrite(url,body,options);
body = struct('method','get_security_info','token',token,'code','600519.XSHG');
options = weboptions('RequestMethod','post','ContentReader',@readtable, 'MediaType','application/json');
result = webwrite(url,body,options);
% writetable(result,'xxxxxx.csv')
result

code,display_name,name,start_date,end_date,type,parent
600519.XSHG,贵州茅台,GZMT,2001-08-27,2200-01-01,stock,

R调用JQData HTTP示例

library(httr)
url <- "https://dataapi.joinquant.com/apis"
body <- list(
    method = "get_token",
    mob = "135xxxxxxx",
    pwd = "xxxxxxxxxx"
)
r <- POST(url, body = body, encode = "json")
token <- content(r)
body <- list(
    method = "get_security_info",
    token = token,
    code = "600519.XSHG"
)
r <- POST(url, body = body, encode = "json")
content(r)

code,display_name,name,start_date,end_date,type,parent
600519.XSHG,贵州茅台,GZMT,2001-08-27,2200-01-01,stock,

C#调用JQData HTTP示例:

using System;
using System.Net.Http;
using System.Text;
using System.Web.Script.Serialization;

namespace JQData.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            QuerySecurityInfo();
        }
        static void QuerySecurityInfo()
        {
            var url = "https://dataapi.joinquant.com/apis";
            using (var client = new HttpClient())
            {
                //需要添加System.Web.Extensions
                //生成JSON请求信息
                string json = new 
                JavaScriptSerializer().Serialize(new
                {
                    method = "get_token",
                    mob = "135xxxxxxx", //mob是申请JQData时所填写的手机号
                    pwd = "xxxxxxxxxx" //Password为聚宽官网登录密码,新申请用户默认为手机号后6位
                });
                var content = new StringContent(json,
                 Encoding.UTF8, "application/json");
                //POST请求并等待结果
                var result = client.PostAsync(url, 
                content).Result;
                //读取返回的TOKEN
                string token = result.Content.ReadAsStringAsync().Result;
                string body = new JavaScriptSerializer().Serialize(new
                {
                    method = "get_security_info",
                    token = token, //token
                    code = "600519.XSHG" //代码
                });
                var bodyContent = new StringContent(body,
                 Encoding.UTF8, "application/json");
                //POST请求并等待结果
                result = client.PostAsync(url, 
                bodyContent).Result;
                //code,display_name,name,start_date,end_date,type,parent
                //600519.XSHG,贵州茅台,GZMT,2001-08-27,2200-01-01,stock,
                var securityInfo = result.Content.ReadAsStringAsync().Result;
                Console.WriteLine("Result\n" + securityInfo);
            }
        }
    }
}

更多语言示例详见 JQData HTTP说明文档。使用中如有任何问题请添加JQData管理员咨询解答,扫描二维码立刻添加。
Img

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

以下是几种调取 HTTP 接口传输 Excel 文件的方法: ### 后台将文件通过输出流输出到前端 在 Java 中可以使用 Apache POI 库生成 Excel 文件,并通过 HTTP 响应将文件输出到前端供用户下载。示例代码如下: ```java import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.net.URLEncoder; public class ExcelDownload { public static void downloadExcel(HttpServletResponse response, String filename, File file) throws IOException { Workbook workbook = new XSSFWorkbook(file); ServletOutputStream output = null; if (workbook != null) { output = response.getOutputStream(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType("application/msexcel"); workbook.write(output); } if (workbook != null) { workbook.close(); } if (output != null) { output.close(); } } } ``` 这种方式适用于在 Web 应用中,用户请求下载 Excel 文件的场景,通过设置响应头和输出流将文件发送给客户端,客户端浏览器会弹出下载提示框让用户保存文件。 ### 使用 `HttpURLConnection` 传递文件及参数 可以使用 `HttpURLConnection` 来传递 Excel 文件及相关参数。示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class FileUpload { public static void uploadFile(String urlStr, String filePath, String paramName) throws IOException { File file = new File(filePath); URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); String boundary = "---------------------------" + System.currentTimeMillis(); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream outputStream = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); writer.append("--").append(boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"").append(paramName).append("\"; filename=\"") .append(URLEncoder.encode(file.getName(), "UTF-8")).append("\"\r\n"); writer.append("Content-Type: ").append("application/vnd.ms-excel").append("\r\n"); writer.append("Content-Transfer-Encoding: binary\r\n"); writer.append("\r\n"); writer.flush(); FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); inputStream.close(); writer.append("\r\n"); writer.append("--").append(boundary).append("--").append("\r\n"); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); System.out.println("Server response: " + response.toString()); } connection.disconnect(); } } ``` 这种方式适用于需要将 Excel 文件上传到服务器的场景,需要注意的是,一旦将 `Content-Type` 设置为 `multipart/form-data`,原先通过键值对获取参数的方式就不再可行,可以通过传递一个仅记录文件名即所需传递参数的空文件,然后在服务端通过获取文件的文件名然后将其解析成参数。 ### 使用 Apache HttpClient 上传文件 ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.File; import java.io.IOException; public class HttpClientFileUpload { public static void uploadFile(String urlStr, String filePath) throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(urlStr); File file = new File(filePath); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", file); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { System.out.println(EntityUtils.toString(responseEntity)); } } } ``` 这种方式也是用于将 Excel 文件上传到服务器,使用 Apache HttpClient 库可以更方便地处理 HTTP 请求和响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值