基金收益,通过Java实时获取
前言:
今天要向大家分享一些干貨內(nèi)容,關(guān)于Java實時獲取收益源碼,看過的人都會收藏。
第一,JAVA爬取天天基金網(wǎng)數(shù)據(jù)使用實例
第二,應(yīng)用技巧
第三,基本知識點總結(jié)和需要注意事項
Java爬蟲實時獲取基金收益歷史記錄代碼:
首先要自己定義幾個參數(shù):基金編碼,頁數(shù),每頁顯示條數(shù) 開始時間結(jié)束時間等
(我這直接寫的靜態(tài)方法使用的 大家可以改成Test方法自行進行測試)
?/**
? ? ?* httClient 請求 GET
? ? ?* 獲取基金網(wǎng)數(shù)據(jù)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返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
? ? ? ? int indexStart = str.indexOf("(");
? ? ? ? System.out.println(indexStart);
? ? ? ? //截取字符串
? ? ? ? str = str.substring(indexStart + 9, length - 90);
? ? ? ? System.out.println(str);
? ? ? ? //轉(zhuǎn)換為Obj類型
? ? ? ? JSONObject jsonObject = JSON.parseObject(str);
? ? ? ? System.out.println(jsonObject);
? ? ? ? //獲取數(shù)組
? ? ? ? JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");
? ? ? ? //計算數(shù)組的長度
? ? ? ? int size = jsonArray.size();
? ? ? ? System.out.println(size);
?
? ? ? ? return jsonArray;
? ? }
通過基金編碼查詢基金名稱
?/**
? ? ?* httClient 請求 GET
? ? ?* 獲取基金網(wǎng)數(shù)據(jù)2
? ? ?*/
? ? @Test
? ? public static String testDepartmentList2(String code) {
? ? ? ? //數(shù)據(jù)鏈接
? ? ? ? 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返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
? ? ? ? int i = str.indexOf("(");
? ? ? ? System.out.println(i);
? ? ? ? //截取字符串
? ? ? ? str = str.substring(i + 55, length - 3);
? ? ? ? System.out.println(str);
? ? ? ? //轉(zhuǎn)換為Obj類型
? ? ? ? JSONObject jsonObject = JSON.parseObject(str);
? ? ? ? System.out.println(jsonObject);
? ? ? ? String fundName = jsonObject.getString("f58");
? ? ? ? return fundName;
? ? }
java實時獲取基金收益
業(yè)務(wù)層實現(xiàn):(主要功能:用戶輸入基金編號查詢某個基金時如果數(shù)據(jù)庫沒有,自動從天天基金網(wǎng)爬取數(shù)據(jù)存儲到數(shù)據(jù)庫并顯示到頁面上)
顯示的數(shù)據(jù)分別有:基金編號 基金日期 基金名稱 實際價格 每日漲跌幅
?@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);
? ? ? ? ? ? //計算數(shù)組的長度
? ? ? ? ? ? int size = jsonArray.size();
? ? ? ? ? ? System.out.println(size);
? ? ? ? ? ? //for循環(huán)遍歷
? ? ? ? ? ? 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");
? ? ? ? ? ? ? ? //創(chuàng)建時間
? ? ? ? ? ? ? ? DateTime dateTime = new DateTime();
? ? ? ? ? ? ? ? //獲取創(chuàng)建時間
? ? ? ? ? ? ? ? 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層
?/**
? ? ?* 基金頁面數(shù)據(jù)交互
? ? ?* @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
?* 獲取基金網(wǎng)數(shù)據(jù)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返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
int indexStart = str.indexOf("(");
? ? System.out.println(indexStart);
? ? //截取字符串
str = str.substring(indexStart + 9, length - 90);
? ? System.out.println(str);
? ? //轉(zhuǎn)換為Obj類型
JSONObject jsonObject = JSON.parseObject(str);
? ? System.out.println(jsonObject);
? ? //獲取數(shù)組
JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");
? ? //計算數(shù)組的長度
int size = jsonArray.size();
? ? System.out.println(size);
?
? ? return jsonArray;
}
最后:
以上就是我為大家分享的干貨內(nèi)容,還有很多免費的學(xué)習(xí)資料和大廠面試資料,想要的可以私信我,我免費分享給大家~
總結(jié)
以上是生活随笔為你收集整理的基金收益,通过Java实时获取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows程序设计(第2版 王艳平)
- 下一篇: OKR真正有效的的4个最佳实践 [OKR