JAVA爬取天天基金网数据

天天基金网网址:http://quote.eastmoney.com/center/gridlist.html#fund_lof

爬取基金历史记录代码:

1。首先要自己定义几个参数:基金编码,页数,每页显示条数 开始时间结束时间等

(我这直接写的静态方法使用的 大家可以改成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;
    }

通过基金编码查询基金名称

(由于查询的基金历史日期url里面的信息只有基金编号跟涨跌幅日期等 没有基金名称 我们通过基金网的查询功能自行填充基金编码进行查询)

/**
     * 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;
    }

业务层实现:(主要功能:用户输入基金编号查询某个基金时如果数据库没有,自动从天天基金网爬取数据存储到数据库并显示到页面上)

显示的数据分别有:基金编号 基金日期 基金名称 实际价格 每日涨跌幅

@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;
    }

运行效果如图:

(根据基金编号进行查询基金 如果数据库没有则自动从天天基金网拉取数据并显示到页面上 共拉取20条历史数据)

JAVA爬取天天基金网数据

JAVA爬取天天基金网数据

@注:本博客仅为个人学习笔记。 所属人:Yuan
/** * 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;}

相关推荐