1.背景
2.数据获取分析
3.代码获取数据
代码:
package com.life.gupiao;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import java.util.*;
/**
* @Copyright (C) XXXXX技有限公司
* @Author: ldp
* @Date: 2023/10/25 10:57
* @Description:
*/
@SuppressWarnings(value = "all")
public class DataDemo {
/**
* 数据获取
*/
@Test
public void test01() {
String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json";
HttpRequest request = HttpUtil.createGet(url);
Map<String, Object> map = new HashMap<>(8);
map.put("symbol", "SH000001"); // 股票编号,SH000001
map.put("begin", getTimeLong("2023-10-24")); // 开始时间毫秒 1698288935706
map.put("period", "day"); // 日k:day,周k,week月k:month
map.put("type", "before");
map.put("count", -365 * 20);
//kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance
map.put("indicator", "kline");
request.form(map);
request.cookie("device_id=efb20afd8a6af0e23aa5a234e0a379c5; s=c9115tfaqz; xq_a_token=e2f0876e8fd368a0be2b6d38a49ed2dd5eec7557; xqat=e2f0876e8fd368a0be2b6d38a49ed2dd5eec7557; xq_r_token=2a5b753b2db675b4ac36c938d20120660651116d; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcwMDY5OTg3NSwiY3RtIjoxNjk4MjAyMzEzNzAwLCJjaWQiOiJkOWQwbjRBWnVwIn0.LQL_AFe1BAi48kE09x4htcBmrX0fWGxISLDIm8zOJbZ_A6vQ0rrAvirmpUmTn1hJDXtNmHZEX0SIlbncsmAmZXNjtXBJ9Du7XhW8zSB32tuGChOX4lPsgcrGlo9_35GPAVvza1bGDoEtZTV8IwfmOkUbh47KWrXk1LzFzVk_-E-WAsobxxGIucFD26UNAYABSoOuvJKVUFBnWfQOocDJZRYtefcEO64Zb1Sj7H5Gja3SJPS6IMqKED4lKlX7XHZC0NBy9v_kGnXehbmu7fcPrz6pymLvaQ6y_QZI2Oye2dveoQN3Khdj3uUBFlFHyWeDGaJoFo5WImXxC0Wd8kjvBw; cookiesu=721698202320000; u=721698202320000; Hm_lvt_1db88642e346389874251b5a1eded6e3=1698202321; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1698202413");
String body = request.execute().body();
System.out.println(body);
JSONObject jsonObject = JSONObject.parseObject(body).getJSONObject("data");
JSONArray column = jsonObject.getJSONArray("column");
JSONArray item = jsonObject.getJSONArray("item");
String symbol = jsonObject.getString("symbol");
createEcxel(symbol, column, item);
}
/**
* 方法:存入excel
*/
private void createEcxel(String symbol, JSONArray column, JSONArray item) {
System.out.println("文件-处理-开始");
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < item.size(); i++) {
JSONArray jsonArray = item.getJSONArray(i);
Map<String, Object> map = new HashMap<>();
map.put("symbol", symbol);
for (int j = 0; j < jsonArray.size(); j++) {
String columnName = column.getString(j);
if (columnName.equals("timestamp")) {
// 时间转为yyyy-MM-dd HH:mm:ss
Long aLong = jsonArray.getLong(j);
Date date = new Date(aLong);
String year = DateUtil.format(date, "yyyy");
map.put("year_str", year);
String month = DateUtil.format(date, "yyyy-MM");
map.put("month_str", month);
String format = DateUtil.format(date, "yyyy-MM-dd");
map.put("date_str", format);
} else {
map.put(column.getString(j), jsonArray.get(j));
}
}
list.add(map);
}
String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");
ExcelWriter writer = ExcelUtil.getWriter("D:\\workroom\\myJavaDemo\\life\\" + symbol + "_" + format + "_.xls");
writer.write(list);
writer.flush();
writer.close();
System.out.println("文件-处理-完成");
}
/**
* 方法:时间转换
*/
private Long getTimeLong(String dateStr) {
DateTime parse = DateUtil.parse(dateStr, "yyyy-MM-dd");
long time = parse.getTime();
return time;
}
}
执行代码得到excel数据如下:
执行代码得到excel数据,部分截图如下:
4.数据入库
将excel数据导入数据库
截图如下:
5.数据分析
根据自己的需求分析数据
这里给出简单的案例sql
-- 查询所有
select t.* from LDP_TEST2 t order by t.date_str;
-- 查询所有日期,按照日期倒序
select t.date_str,t.high,t.low from LDP_TEST2 t order by t.date_str;
-- 按年 找最高,最低,年排序
select min(t.symbol) as 编号,
t.year_str as 年份,
max(t.high) 最高,
min(t.low) 最低
from LDP_TEST2 t
group by t.year_str
order by 年份;
-- 按年 找最高,最低,最低排序
select *
from (select min(t.symbol) as 编号,
t.year_str as 年份,
max(t.high) 最高,
min(t.low) 最低
from LDP_TEST2 t
group by t.year_str) temp
order by 最低;
-- 按年 找最高,最低,最高排序
select *
from (select min(t.symbol) as 编号,
t.year_str as 年份,
max(t.high) 最高,
min(t.low) 最低
from LDP_TEST2 t
group by t.year_str) temp
order by 最高;
5.1.案例1
5.2.案例2
5.3.案例3
其他,可以根据自己需求做