Java實時獲取基金收益項目源碼分享
本文章向大家介紹JAVA爬取天天基金網(wǎng)數(shù)據(jù),主要包括JAVA爬取天天基金網(wǎng)數(shù)據(jù)使用實例、應(yīng)用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。
天天基金網(wǎng)網(wǎng)址:http://quote.eastmoney.com/center/gridlist.html#fund_lof

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;
}
通過基金編碼查詢基金名稱
(由于基金網(wǎng)url里面的信息只有基金編號跟漲跌幅日期等 沒有基金名稱 我們通過基金網(wǎng)的查詢功能自行填充基金編碼進行查詢)
/**
* 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實時獲取基金收益項目運行效果如圖:



(根據(jù)基金編號進行查詢基金 如果數(shù)據(jù)庫沒有則自動從天天基金網(wǎng)拉取數(shù)據(jù)并顯示到頁面上 共拉取20條歷史數(shù)據(jù))
/**
* 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;
}
這就是我為大家分享的Java實時獲取基金收益項目源碼,希望對大家有幫助哈~~~
到此這篇關(guān)于Java實時獲取基金收益項目源碼分享的文章就介紹到這了,更多相關(guān)Java獲取基金收益內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot使用redis實現(xiàn)接口Api限流的示例代碼
本文主要介紹了Springboot使用redis實現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-07-07
Mybatis Properties 配置優(yōu)先級詳解
這篇文章主要介紹了Mybatis Properties 配置優(yōu)先級,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

