日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

基金收益,通过Java实时获取

發布時間:2023/12/29 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基金收益,通过Java实时获取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

今天要向大家分享一些干貨內容,關于Java實時獲取收益源碼,看過的人都會收藏。

第一,JAVA爬取天天基金網數據使用實例

第二,應用技巧

第三,基本知識點總結和需要注意事項

Java爬蟲實時獲取基金收益歷史記錄代碼:

首先要自己定義幾個參數:基金編碼,頁數,每頁顯示條數 開始時間結束時間等

(我這直接寫的靜態方法使用的 大家可以改成Test方法自行進行測試)

?/**
? ? ?* httClient 請求 GET
? ? ?* 獲取基金網數據1
? ? ?*/
? ? public static JSONArray testDepartmentList1(String code){
? ? ? ? Integer pageIndex = 1;
? ? ? ? Integer pageSize=20;
? ? ? ? String startTime="2018-1-1";
? ? ? ? String endTime = "2020-4-15";
? ? ? ? String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";
? ? ? ? long time = System.currentTimeMillis();
? ? ? ? String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18306596328894644803_1571038362181&" +
? ? ? ? ? ? ? ? "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";
? ? ? ? url = String.format(url,code,pageIndex,pageSize,startTime,endTime,time);
? ? ? ? System.out.println("url= " + url);
? ? ? ? System.out.println(url);
? ? ? ? HttpRequest request = HttpUtil.createGet(url);
? ? ? ? request.header("Referer", referer);
? ? ? ? String str = request.execute().body();
? ? ? ? //獲取str的長度
? ? ? ? System.out.println("str=" + str);
? ? ? ? int length = str.length();
? ? ? ? System.out.println("length=" + length);
? ? ? ? //indexOf返回某個指定的字符串值在字符串中首次出現的位置
? ? ? ? int indexStart = str.indexOf("(");
? ? ? ? System.out.println(indexStart);
? ? ? ? //截取字符串
? ? ? ? str = str.substring(indexStart + 9, length - 90);
? ? ? ? System.out.println(str);
? ? ? ? //轉換為Obj類型
? ? ? ? JSONObject jsonObject = JSON.parseObject(str);
? ? ? ? System.out.println(jsonObject);
? ? ? ? //獲取數組
? ? ? ? JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");
? ? ? ? //計算數組的長度
? ? ? ? int size = jsonArray.size();
? ? ? ? System.out.println(size);
?
? ? ? ? return jsonArray;
? ? }
通過基金編碼查詢基金名稱

?/**
? ? ?* httClient 請求 GET
? ? ?* 獲取基金網數據2
? ? ?*/
? ? @Test
? ? public static String testDepartmentList2(String code) {
? ? ? ? //數據鏈接
? ? ? ? String referer = "http://so.eastmoney.com/web/s?keyword="+code+"";
? ? ? ? ?long time = System.currentTimeMillis();
?
? ? ? ? String url = "http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&fltt" +
? ? ? ? ? ? ? ? "=2&fields=f59,f169,f170,f161,f163,f171,f126,f168,f164,f78,f162,f43,f46,f44,f45,f60,f47," +
? ? ? ? ? ? ? ? "f48,f49,f84,f116,f55,f92,f71,f50,f167,f117,f85,f84,f58,f57,f86,f172,f108,f118,f107,f164," +
? ? ? ? ? ? ? ? "f177&invt=2&secid=0."+code+"&cb=jQuery1124006112441213993569_1587006450385&_=1587006450403";
? ? ? ? url = String.format(url,code);
? ? ? ? System.out.println("請求url:" + url);
? ? ? ? //http請求
? ? ? ? HttpRequest request = HttpUtil.createGet(url);
?
? ? ? ? request.header("Referer", referer);
? ? ? ? String str = request.execute().body();
? ? ? ? //獲取str的長度
? ? ? ? System.out.println("str=" + str);
? ? ? ? int length = str.length();
? ? ? ? System.out.println("length=" + length);
? ? ? ? //indexOf返回某個指定的字符串值在字符串中首次出現的位置
? ? ? ? int i = str.indexOf("(");
? ? ? ? System.out.println(i);
? ? ? ? //截取字符串
? ? ? ? str = str.substring(i + 55, length - 3);
? ? ? ? System.out.println(str);
? ? ? ? //轉換為Obj類型
? ? ? ? JSONObject jsonObject = JSON.parseObject(str);
? ? ? ? System.out.println(jsonObject);
? ? ? ? String fundName = jsonObject.getString("f58");
? ? ? ? return fundName;
? ? }
java實時獲取基金收益

業務層實現:(主要功能:用戶輸入基金編號查詢某個基金時如果數據庫沒有,自動從天天基金網爬取數據存儲到數據庫并顯示到頁面上)

顯示的數據分別有:基金編號 基金日期 基金名稱 實際價格 每日漲跌幅

?@Override
? ? public List<FundHistory> query(String fundCode) {
? ? ? ? List<FundHistory> query = fundHistoryDao.query(fundCode);
? ? ? ? if (query.size()==0) {
? ? ? ? ? ? JSONArray jsonArray = testDepartmentList1(fundCode);
? ? ? ? ? ? System.out.println(jsonArray);
? ? ? ? ? ? //計算數組的長度
? ? ? ? ? ? int size = jsonArray.size();
? ? ? ? ? ? System.out.println(size);
? ? ? ? ? ? //for循環遍歷
? ? ? ? ? ? for (int j = 0; j < size; j++) {
? ? ? ? ? ? ? ? JSONObject jsonObject1 = jsonArray.getJSONObject(j);
? ? ? ? ? ? ? ? //獲取凈值日期
? ? ? ? ? ? ? ? String date = jsonObject1.getString("FSRQ");
? ? ? ? ? ? ? ? //獲取單位凈值
? ? ? ? ? ? ? ? Double unit = jsonObject1.getDouble("DWJZ");
? ? ? ? ? ? ? ? //獲取累積凈值
? ? ? ? ? ? ? ? Double Accumulates = jsonObject1.getDouble("LJJZ");
? ? ? ? ? ? ? ? //獲取日增長率
? ? ? ? ? ? ? ? String growthRate = jsonObject1.getString("JZZZL");
? ? ? ? ? ? ? ? //創建時間
? ? ? ? ? ? ? ? DateTime dateTime = new DateTime();
? ? ? ? ? ? ? ? //獲取創建時間
? ? ? ? ? ? ? ? String datetime = String.valueOf(dateTime);
? ? ? ? ? ? ? ? FundHistory fundHistory = new FundHistory();
? ? ? ? ? ? ? ? fundHistory.setFundCode(fundCode);
? ? ? ? ? ? ? ? fundHistory.setDate(date);
? ? ? ? ? ? ? ? fundHistory.setUnit(unit);
? ? ? ? ? ? ? ? fundHistory.setAccumulates(Accumulates);
? ? ? ? ? ? ? ? fundHistory.setGrowthRate(growthRate);
? ? ? ? ? ? ? ? fundHistory.setCreateTime(datetime);
? ? ? ? ? ? ? ? fundHistoryDao.saveFundHistory(fundHistory);
? ? ? ? ? ? }
? ? ? ? ? ? FundHistory fundHistory = new FundHistory();
? ? ? ? ? ? fundHistory.setFundCode(fundCode);
? ? ? ? ? ? //獲取基金名稱
? ? ? ? ? ? String fundName = testDepartmentList2(fundCode);
? ? ? ? ? ? fundHistory.setFundName(fundName);
? ? ? ? ? ? fundHistoryDao.updateFundHistory(fundHistory);
? ? ? ? ? ? List<FundHistory> query2 = fundHistoryDao.query(fundCode);
? ? ? ? ? ? FundHistory fundHistory1 = query2.get(0);
? ? ? ? ? ? fundDao.saveFund2(fundHistory1);
? ? ? ? ? ? return query2;
? ? ? ? }
? ? ? ? return query;
? ? }
controller層

?/**
? ? ?* 基金頁面數據交互
? ? ?* @param
? ? ?* @return
? ? ?*/
? ? @RequestMapping("/enquiryfund")
? ? @ResponseBody
? ? public Result enquiryfund(String fundCode,String fundName){
? ? ? ? Result result = new Result<>();
? ? ? ? if (fundCode!=""){
? ? ? ? ? ? List<FundHistory> query = fundHistoryService.query(fundCode);
? ? ? ? ? ? if (query==null){
? ? ? ? ? ? ? ? List<FundHistory> query2 = fundHistoryService.query(fundCode);
? ? ? ? ? ? ? ? result.setData(query2);
? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? }
? ? ? ? ? ? result.setData(query);
? ? ? ? ? ? return result;
? ? ? ? }else if (fundName!=""){
? ? ? ? ? ? List<FundHistory> fundHistories = fundHistoryService.query2(fundName);
? ? ? ? ? ? result.setData(fundHistories);
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ? return result;
? ? }

java實時獲取基金收益項目運行效果如圖:


/**
?* httClient 請求 GET
?* 獲取基金網數據1
?*/
?
?
?
public static JSONArray testDepartmentList1(String code){
? ? Integer pageIndex = 1;
? ? Integer pageSize=20;
? ? String startTime="2018-1-1";
? ? String endTime = "2020-4-15";
? ? String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";
? ? long time = System.currentTimeMillis();
? ? String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18306596328894644803_1571038362181&" +
? ? ? ? ? ? "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";
? ? url = String.format(url,code,pageIndex,pageSize,startTime,endTime,time);
? ? System.out.println("url= " + url);
? ? System.out.println(url);
? ? HttpRequest request = HttpUtil.createGet(url);
? ? request.header("Referer", referer);
? ? String str = request.execute().body();
? ? //獲取str的長度
System.out.println("str=" + str);
? ? int length = str.length();
? ? System.out.println("length=" + length);
? ? //indexOf返回某個指定的字符串值在字符串中首次出現的位置
int indexStart = str.indexOf("(");
? ? System.out.println(indexStart);
? ? //截取字符串
str = str.substring(indexStart + 9, length - 90);
? ? System.out.println(str);
? ? //轉換為Obj類型
JSONObject jsonObject = JSON.parseObject(str);
? ? System.out.println(jsonObject);
? ? //獲取數組
JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");
? ? //計算數組的長度
int size = jsonArray.size();
? ? System.out.println(size);
?
? ? return jsonArray;
}

最后:

以上就是我為大家分享的干貨內容,還有很多免費的學習資料和大廠面試資料,想要的可以私信我,我免費分享給大家~

總結

以上是生活随笔為你收集整理的基金收益,通过Java实时获取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。