java 爬取数据_JAVA爬取天天基金网数据

本文展示了如何使用Java编程爬取天天基金网的基金历史数据。首先定义了爬取参数,如基金编码、页数、起止时间等,然后通过HttpClinet发送GET请求,处理响应数据,提取JSON内容,包括基金的历史净值、涨跌幅等信息,并将数据保存到数据库中。此外,还提供了查询基金名称的方法。如果数据库中不存在基金数据,程序会自动从网站爬取并更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

爬取基金历史记录代码:

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

(我这直接写的静态方法使用的 大家可以改成Test方法自行进行测试)

/*** httClient 请求 GET

* 获取基金网数据1*/

public staticJSONArray 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);returnjsonArray;

}

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

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

/*** httClient 请求 GET

* 获取基金网数据2*/@Testpublic staticString 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");returnfundName;

}

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

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

@Overridepublic Listquery(String fundCode) {

List 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 = newDateTime();//获取创建时间

String datetime =String.valueOf(dateTime);

FundHistory fundHistory= newFundHistory();

fundHistory.setFundCode(fundCode);

fundHistory.setDate(date);

fundHistory.setUnit(unit);

fundHistory.setAccumulates(Accumulates);

fundHistory.setGrowthRate(growthRate);

fundHistory.setCreateTime(datetime);

fundHistoryDao.saveFundHistory(fundHistory);

}

FundHistory fundHistory= newFundHistory();

fundHistory.setFundCode(fundCode);//获取基金名称

String fundName =testDepartmentList2(fundCode);

fundHistory.setFundName(fundName);

fundHistoryDao.updateFundHistory(fundHistory);

List query2 =fundHistoryDao.query(fundCode);

FundHistory fundHistory1= query2.get(0);

fundDao.saveFund2(fundHistory1);returnquery2;

}returnquery;

}

controller层

/*** 基金页面数据交互

*@param*@return

*/@RequestMapping("/enquiryfund")

@ResponseBodypublicResult enquiryfund(String fundCode,String fundName){

Result result= new Result<>();if (fundCode!=""){

List query =fundHistoryService.query(fundCode);if (query==null){

List query2 =fundHistoryService.query(fundCode);

result.setData(query2);returnresult;

}

result.setData(query);returnresult;

}else if (fundName!=""){

List fundHistories =fundHistoryService.query2(fundName);

result.setData(fundHistories);returnresult;

}returnresult;

}

运行效果如图:

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

20200416130405286136.png

20200416130405529285.png

@注:本博客仅为个人学习笔记。 所属人:Yuan

/*** httClient 请求 GET* 获取基金网数据1*/public staticJSONArray 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";

longtime = 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);

intlength = str.length();

System.out.println("length="+ length);

//indexOf返回某个指定的字符串值在字符串中首次出现的位置intindexStart = 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");

//计算数组的长度intsize = jsonArray.size();

System.out.println(size);

returnjsonArray;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值