package FixtureAcquisition;//赛程获取
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 爬取赛程
* */
public class CrawlSchedule {
public static void main(String[] args) {
String htmlUrl = "https://webapi.sporttery.cn/gateway/lottery/getFootBallMatchV1.qry?param=90,0&lotteryDrawNum=23012&sellStatus=0&termLimits=11";//网址路径
String json = getJson(htmlUrl);
System.out.println(json);
System.out.println(getMatchMessage(json));
}
/**
* 获取json字符串
* */
private static String getJson(String path) {
// 保存整个html文档的数据
StringBuffer html = new StringBuffer();
try {
// 发起一个url网址的请求
URL url = new URL(path);
URLConnection connection = url.openConnection();
// 设置请求头
connection.addRequestProperty("Accept", "application/json");
// 获取网页的数据流
InputStream input = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(input, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
// 解析并且获取InputStream中具体的数据,并且输出到控制台
String line = "";
while ((line = bufferedReader.readLine()) != null) {
html.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return html.toString();
}
/**
* 处理json字符串,这部分因需求而异,这里处理比较繁琐也比较占用内存资源(因为循环遍历创建map集合)
* */
public static String getMatchMessage(String str){
String str1 = str.substring(0, str.indexOf("sfcMatch"));
String str2 = str.substring(str1.length() + 1, str.length());
String str3 = "{s" + str2;
str3 = str3.substring(0,str3.length() - 1);
String str4 = str3.substring(0,str3.indexOf("matchList")-1);
String str5 = "{" + str3.substring(str4.length(),str3.length()) + "}";
str5 = str5.substring(0,str5.indexOf("sfclist") - 2);
// str5.substring(0,str5.length() - 22)
JSONObject jsonObject = JSON.parseObject(str5);
System.out.println(jsonObject);
JSONObject json1 = (JSONObject) JSONObject.toJSON(jsonObject);
JSONArray temp = JSONArray.parseArray(json1.getString("matchList"));
List lists = new ArrayList<>();
for(int i = 0;i < temp.size();i++){
Map<String,String> map = new HashMap<>();
JSONObject obj = (JSONObject)temp.get(i);
map.put("联赛",obj.getString("matchName"));
map.put("场次",obj.getString("matchNum"));
map.put("主队",obj.getString("masterTeamName"));
map.put("客队",obj.getString("guestTeamName"));
map.put("开赛时间",obj.getString("startTime"));
lists.add(map);
}
String json = JSON.toJSON(lists).toString();//Java中的list转为json
return json;
}
}
Java获取静态网页json数据,解析需要数据并以json格式返回
于 2023-02-03 19:25:42 首次发布