Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享
本文章向大家介紹JAVA爬取天天基金網(wǎng)數(shù)據(jù),主要包括JAVA爬取天天基金網(wǎng)數(shù)據(jù)使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。
天天基金網(wǎng)網(wǎng)址:http://quote.eastmoney.com/center/gridlist.html#fund_lof
Java爬蟲實(shí)時(shí)獲取基金收益歷史記錄代碼:
首先要自己定義幾個(gè)參數(shù):基金編碼,頁數(shù),每頁顯示條數(shù) 開始時(shí)間結(jié)束時(shí)間等
(我這直接寫的靜態(tài)方法使用的 大家可以改成Test方法自行進(jìn)行測試)
/** * 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返回某個(gè)指定的字符串值在字符串中首次出現(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"); //計(jì)算數(shù)組的長度 int size = jsonArray.size(); System.out.println(size); return jsonArray; }
通過基金編碼查詢基金名稱
(由于基金網(wǎng)url里面的信息只有基金編號(hào)跟漲跌幅日期等 沒有基金名稱 我們通過基金網(wǎng)的查詢功能自行填充基金編碼進(jìn)行查詢)
/** * 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返回某個(gè)指定的字符串值在字符串中首次出現(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實(shí)時(shí)獲取基金收益
業(yè)務(wù)層實(shí)現(xiàn):(主要功能:用戶輸入基金編號(hào)查詢某個(gè)基金時(shí)如果數(shù)據(jù)庫沒有,自動(dòng)從天天基金網(wǎng)爬取數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫并顯示到頁面上)
顯示的數(shù)據(jù)分別有:基金編號(hào) 基金日期 基金名稱 實(shí)際價(jià)格 每日漲跌幅
@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); //計(jì)算數(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)建時(shí)間 DateTime dateTime = new DateTime(); //獲取創(chuàng)建時(shí)間 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實(shí)時(shí)獲取基金收益項(xiàng)目運(yùn)行效果如圖:
(根據(jù)基金編號(hào)進(jìn)行查詢基金 如果數(shù)據(jù)庫沒有則自動(dòng)從天天基金網(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返回某個(gè)指定的字符串值在字符串中首次出現(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"); //計(jì)算數(shù)組的長度 int size = jsonArray.size(); System.out.println(size); return jsonArray; }
這就是我為大家分享的Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼,希望對大家有幫助哈~~~
到此這篇關(guān)于Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享的文章就介紹到這了,更多相關(guān)Java獲取基金收益內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目中Druid自動(dòng)登錄功能實(shí)現(xiàn)
Druid是Java語言中最好的數(shù)據(jù)庫連接池,Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能,這篇文章主要介紹了SpringBoot項(xiàng)目中Druid自動(dòng)登錄功能實(shí)現(xiàn),需要的朋友可以參考下2024-08-08Java利用Request請求如何獲取IP地址對應(yīng)的省份、城市詳解
之前已經(jīng)給大家介紹了關(guān)于Java用Request請求獲取IP地址的相關(guān)內(nèi)容,那么下面這篇文章將給大家進(jìn)入深入的介紹,關(guān)于Java利用Request請求如何獲取IP地址對應(yīng)省份、城市的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10Spring中自帶的@Schedule實(shí)現(xiàn)自動(dòng)任務(wù)的過程解析
這篇文章主要介紹了關(guān)于Spring中自帶的@Schedule實(shí)現(xiàn)自動(dòng)任務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06詳解Java接口簽名(Signature)實(shí)現(xiàn)方案
這篇文章主要介紹了Java接口簽名(Signature)實(shí)現(xiàn)方案?,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01SpringMVC+Mybatis實(shí)現(xiàn)的Mysql分頁數(shù)據(jù)查詢的示例
本篇文章主要介紹了SpringMVC+Mybatis實(shí)現(xiàn)的Mysql分頁數(shù)據(jù)查詢的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Spring Boot 整合 Shiro+Thymeleaf過程解析
這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Spring如何按業(yè)務(wù)模塊輸出日志到不同的文件詳解
最近做項(xiàng)目時(shí)有一個(gè)記錄操作日志的需求,比如某個(gè)用戶進(jìn)行了查詢、刪除、修改等操作,下面這篇文章主要給大家介紹了關(guān)于Spring如何按業(yè)務(wù)模塊輸出日志到不同文件的相關(guān)資料,需要的朋友可以參考下2022-05-05springmvc接口接收參數(shù)與請求參數(shù)格式的整理
這篇文章主要介紹了springmvc接口接收參數(shù)與請求參數(shù)格式的整理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11