Fetch

1.pom.xml

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dream</groupId>
<artifactId>fetch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fetch</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.27.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

2.FetchApplication.java

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@SpringBootApplication
@EnableScheduling
public class FetchApplication {

public static void main(String[] args) {
SpringApplication.run(FetchApplication.class, args);
}
}

2.application.properties

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spring.application.name=fetch

3.Fetch

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import jakarta.annotation.PostConstruct;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.Yaml;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class Fetch {

private MatchUtils matchUtils;

private TeamUtils teamUtils;

public Fetch(MatchUtils matchUtils, TeamUtils teamUtils) {
this.matchUtils = matchUtils;
this.teamUtils = teamUtils;
}

@PostConstruct
public void start() throws IOException, URISyntaxException {
Yaml yaml = new Yaml();
InputStream resource = Fetch.class.getClassLoader().getResourceAsStream("fetch.yaml");
HashMap<String, Object> param = yaml.load(resource);

String match = param.get("bet").toString();
String team = param.get("bet").toString();
int days = (int)param.get("days");
List<LinkedHashMap<String, String>> list = (List<LinkedHashMap<String, String>>)param.get("param");

for (HashMap<String, String> map : list) {
String url = map.get("url").toString();
String file = map.get("file").toString();
this.load(url, file);

}

// 更新比赛信息
matchUtils.fetchTodayMatch(match, list, days);
matchUtils.fetchLeagueMatch(match, list);
teamUtils.fetchTeam(team, list);
}

public void load(String url, String pageName) throws IOException {
// 设置 EdgeOptions (或 ChromeOptions)
ChromeOptions options = new ChromeOptions(); //或 ChromeOptions
// 初始化 EdgeDriver (或 ChromeDriver)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\mengchen.shao\\Desktop\\BET\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options); //或 WebDriver driver = new ChromeDriver(options);
try {
// 访问网页
driver.get(url);
// 等待网页加载完成 (可以根据实际情况调整等待时间)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); //Java 8+
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));
String html = driver.getPageSource();
FileWriter fileWriter = new FileWriter(pageName);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(html);
writer.flush();
writer.close();
Thread.sleep(3000);

} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();
} finally {
driver.quit(); // 关闭浏览器
}
}
}

3.MatchUtils

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import com.dream.fetch.model.Match;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MatchUtils {

private DateTimeFormatter fromFormat = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm");

private DateTimeFormatter toFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");

private static String getUserAgent() {
List<String> userAgents = Arrays.asList(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"
);
Random random = new Random();
String userAgent = userAgents.get(random.nextInt(userAgents.size()));
return userAgent;
}

private String getHtml(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
String userAgent = getUserAgent();
Request request = new Request.Builder().url(url).header("User-Agent", userAgent).build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
}


public List<Match> fetchTodayMatch(String fileName, List<LinkedHashMap<String, String>> param, int days) throws IOException {
List<Match> matchList = new ArrayList<Match>();
String sheetName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");

LocalDate from = LocalDate.now();
LocalDate to = LocalDate.now().plusDays(days);

for (LinkedHashMap<String, String> hashMap : param) {
String url = hashMap.get("file");
List<Match> tempList = fetchMatchData(url);
matchList.addAll(tempList);
}
List<Match> filerList = matchList.stream().filter(m -> from.compareTo(LocalDateTime.parse(m.getMatchDateTime(), formatter).toLocalDate()) <= 0
&& to.compareTo(LocalDateTime.parse(m.getMatchDateTime(), formatter).toLocalDate()) >= 0).collect(Collectors.toList());
List<Match> sortList = filerList.stream().sorted(Comparator.comparing(Match::getMatchDateTime)).toList();

insertMatchData(sortList, fileName, sheetName);
createCellStyle(fileName, sheetName);
return matchList;
}


public void fetchLeagueMatch(String fileName, List<LinkedHashMap<String, String>> param) throws IOException, URISyntaxException {
List<Match> matchList = new ArrayList<>();
List<Match> scoreList = new ArrayList<>();

for (LinkedHashMap<String, String> hashMap : param) {
String leagueName = hashMap.get("leagueName").toString();
String url = hashMap.get("file").toString();

List<Match> matchTempList = fetchMatchData(url);
insertMatchData(matchTempList, fileName, leagueName);
matchList.addAll(matchTempList);

List<Match> scoreTempList = fetchMatchScore(url);
insertMatchScore(scoreTempList, fileName, leagueName);
scoreList.addAll(scoreTempList);

createCellStyle(fileName, leagueName);
}
}


/**
* 比赛信息
* @param url
* @return
* @throws IOException
*/
public List<Match> fetchMatchData(String url) throws IOException {
List<Match> matchList = new ArrayList<Match>();
Document document = Jsoup.parse(new File(url), "UTF-8");
Element next = document.getElementById("next");
if (next == null) {
System.out.println(url + ":" + "is not any match information!");
return matchList;
}
Elements rows = next.select("tbody > tr");
for (Element row : rows) {
Elements columns = row.select("td");

Match match = new Match();
match.setMatchDateTime(LocalDateTime.parse(columns.get(2).text(), fromFormat).format(toFormat));
match.setLeagueName(columns.get(0).text().trim());
match.setHomeTeam(columns.get(3).select("a").text().trim());
match.setAwayTeam(columns.get(5).select("a").text().trim());

match.setConcedeGreater("-".equals(columns.get(7).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(7).text()).setScale(3, RoundingMode.HALF_UP));
match.setConcede("-".equals(columns.get(8).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(8).text()).setScale(2, RoundingMode.HALF_UP));
match.setConcedeLess("-".equals(columns.get(9).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(9).text()).setScale(3, RoundingMode.HALF_UP));

match.setGoalsGreater("-".equals(columns.get(10).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(10).text()).setScale(3, RoundingMode.HALF_UP));
match.setGoals("-".equals(columns.get(11).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(11).text()).setScale(2, RoundingMode.HALF_UP));
match.setGoalsLess("-".equals(columns.get(12).text().trim()) ? BigDecimal.ZERO : new BigDecimal(columns.get(12).text()).setScale(3, RoundingMode.HALF_UP));

match.setCornerGreater(BigDecimal.ZERO);
match.setCorner(BigDecimal.ZERO);
match.setCornerLess(BigDecimal.ZERO);

match.setHomeHalfScore(0);
match.setHomeFullScore(0);
match.setAwayHalfScore(0);
match.setAwayFullScore(0);
matchList.add(match);
}

return matchList;
}

/**
* 比赛结果
* @param url
* @return
* @throws IOException
*/
public List<Match> fetchMatchScore(String url) throws IOException {
List<Match> matchList = new ArrayList<Match>();
//String html = getHtml(url);
//Document document = Jsoup.parse(html, "UTF-8");
Document document = Jsoup.parse(new File(url), "UTF-8");
Element ended = document.getElementById("ended");
Elements rows = ended.select("tbody > tr");
for (Element row : rows) {
Elements columns = row.select("td");

Match match = new Match();
match.setMatchDateTime(LocalDateTime.parse(columns.get(2).text(), fromFormat).format(toFormat));
match.setLeagueName(columns.get(0).text().trim());
match.setHomeTeam(columns.get(3).select("a").text().trim());
match.setAwayTeam(columns.get(5).select("a").text().trim());

match.setHomeHalfScore(Integer.valueOf(columns.get(6).text().replace(" ", "").split(":")[0]));
match.setHomeFullScore(Integer.valueOf(columns.get(4).text().replace(" ", "").split(":")[0]));
match.setAwayHalfScore(Integer.valueOf(columns.get(6).text().replace(" ", "").split(":")[1]));
match.setAwayFullScore(Integer.valueOf(columns.get(4).text().replace(" ", "").split(":")[1]));
matchList.add(match);
}
return matchList;
}

/**
* 比赛信息数据存入excel中
* @param matchList
* @param fileName
* @throws IOException
*/
public void insertMatchData(List<Match> matchList, String fileName, String sheetName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
XSSFSheet sheet = createMySheet(workbook, sheetName);
System.out.println(String.format("start insertMatchData sheetName = %s", sheet.getSheetName()));
for(Match m : matchList) {
boolean rowHas = false;
for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
XSSFRow row = sheet.getRow(i);
LocalDateTime rowTime = LocalDateTime.parse(row.getCell(0).getStringCellValue(), toFormat);
LocalDateTime matchDateTime = LocalDateTime.parse(m.getMatchDateTime(), toFormat);
String homeTeam = row.getCell(2).getStringCellValue();
String awayTeam = row.getCell(3).getStringCellValue();
if (rowTime.compareTo(matchDateTime) == 0 && homeTeam.equals(m.getHomeTeam()) && awayTeam.equals(m.getAwayTeam())) {
row.getCell(4).setCellValue(m.getConcedeGreater().toString());
row.getCell(5).setCellValue(m.getConcede().toString());
row.getCell(6).setCellValue(m.getConcedeLess().toString());

row.getCell(7).setCellValue(m.getGoalsGreater().toString());
row.getCell(8).setCellValue(m.getGoals().toString());
row.getCell(9).setCellValue(m.getGoalsLess().toString());

row.getCell(10).setCellValue("0:0");
row.getCell(11).setCellValue("0:0");

rowHas = true;
break;
}
}
if (!rowHas) {
/**
* 记录不存在插入新的一条
* 既存整体下移一行,将新数据插入第一行
*/
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
row.createCell(0).setCellValue(m.getMatchDateTime());
row.createCell(1).setCellValue(m.getLeagueName());
row.createCell(2).setCellValue(m.getHomeTeam());
row.createCell(3).setCellValue(m.getAwayTeam());

row.createCell(4).setCellValue(m.getConcedeGreater().toString());
row.createCell(5).setCellValue(m.getConcede().toString());
row.createCell(6).setCellValue(m.getConcedeLess().toString());

row.createCell(7).setCellValue(m.getGoalsGreater().toString());
row.createCell(8).setCellValue(m.getGoals().toString());
row.createCell(9).setCellValue(m.getGoalsLess().toString());

row.createCell(10).setCellValue(m.getHomeHalfScore() + ":" + m.getAwayHalfScore());
row.createCell(11).setCellValue(m.getHomeFullScore() + ":" + m.getAwayFullScore());
}
}

FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}

public void insertMatchScore(List<Match> matchList, String fileName, String sheetName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
XSSFSheet sheet = createMySheet(workbook, sheetName);
System.out.println(String.format("start insertMatchScore sheetName = %s", sheet.getSheetName()));
for(Match m : matchList) {
for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
XSSFRow row = sheet.getRow(i);
String homeTeam = row.getCell(2).getStringCellValue();
String awayTeam = row.getCell(3).getStringCellValue();
if (homeTeam.equals(m.getHomeTeam()) && awayTeam.equals(m.getAwayTeam())) {
row.getCell(10).setCellValue(m.getHomeHalfScore() + ":" + m.getAwayHalfScore());
row.getCell(11).setCellValue(m.getHomeFullScore() + ":" + m.getAwayFullScore());
break;
}
}
}

FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}

public void createCellStyle(String fileName, String sheetName) throws IOException {
System.out.println(String.format("start createCellStyle sheetName = %s", sheetName));
Workbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
Sheet sheet = workbook.getSheet(sheetName);
Font font = workbook.createFont();
font.setFontName("Lucida Sans");
font.setFontHeightInPoints((short) 10);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
for (int i = 0; i < sheet.getLastRowNum() + 1 ; i++) {
Row row = sheet.getRow(i);
row.getCell(0).setCellStyle(cellStyle);
row.getCell(1).setCellStyle(cellStyle);
row.getCell(2).setCellStyle(cellStyle);
row.getCell(3).setCellStyle(cellStyle);
row.getCell(4).setCellStyle(cellStyle);
row.getCell(5).setCellStyle(cellStyle);
row.getCell(6).setCellStyle(cellStyle);
row.getCell(7).setCellStyle(cellStyle);
row.getCell(8).setCellStyle(cellStyle);
row.getCell(9).setCellStyle(cellStyle);
row.getCell(10).setCellStyle(cellStyle);
row.getCell(11).setCellStyle(cellStyle);
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}

public XSSFSheet createMySheet(XSSFWorkbook workbook, String sheetName) throws IOException {
XSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
sheet = workbook.createSheet(sheetName);
sheet.setColumnWidth(0, 22*256);
sheet.setColumnWidth(1, 10*256);
sheet.setColumnWidth(2, 18*256);
sheet.setColumnWidth(3, 18*256);
sheet.setColumnWidth(4, 10*256);
sheet.setColumnWidth(5, 12*256);
sheet.setColumnWidth(6, 10*256);
sheet.setColumnWidth(7, 10*256);
sheet.setColumnWidth(8, 12*256);
sheet.setColumnWidth(9, 10*256);
sheet.setColumnWidth(10, 10*256);
sheet.setColumnWidth(11, 10*256);

XSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("时间");
row.createCell(1).setCellValue("联赛");
row.createCell(2).setCellValue("主队");
row.createCell(3).setCellValue("客队");

row.createCell(4).setCellValue("高");
row.createCell(5).setCellValue("让球");
row.createCell(6).setCellValue("低");

row.createCell(7).setCellValue("高");
row.createCell(8).setCellValue("大小球");
row.createCell(9).setCellValue("低");

row.createCell(10).setCellValue("比分(半)");
row.createCell(11).setCellValue("比分(全)");
}
return sheet;
}
}

3.TeamUtils

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import com.dream.fetch.model.Team;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class TeamUtils {


private DateTimeFormatter fromFormat = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm");

private DateTimeFormatter toFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");

private static String getUserAgent() {
List<String> userAgents = Arrays.asList(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"
);
Random random = new Random();
String userAgent = userAgents.get(random.nextInt(userAgents.size()));
return userAgent;
}

private String getHtml(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
String userAgent = getUserAgent();
Request request = new Request.Builder().url(url).header("User-Agent", userAgent).build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
}

public void fetchTeam(String fileName, List<LinkedHashMap<String, String>> param) throws IOException {
List<Team> teamList = new ArrayList<>();
for (LinkedHashMap<String, String> hashMap : param) {
String url = hashMap.get("file").toString();
String leagueName = hashMap.get("leagueName").toString();
String sheetName = hashMap.get("rank").toString();

List<Team> tempList = fetchTeamData(url, leagueName);
clearTeamData(fileName, sheetName);
insertTeamData(tempList, fileName, sheetName);
teamList.addAll(tempList);
createCellStyle(fileName, sheetName);
}
}


public void clearTeamData(String fileName, String sheetName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
int sheetIndex = workbook.getSheetIndex(workbook.getSheet(sheetName));
if (sheetIndex > 0) {
workbook.removeSheetAt(sheetIndex);
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}

/**
* @return
* @throws IOException
*/
public List<Team> fetchTeamData(String url, String leagueName) throws IOException {
List<Team> teamList = new ArrayList<Team>();
Document document = Jsoup.parse(new File(url), "UTF-8");
Elements teams_all = document.getElementsByClass("teams_all");
Elements rows = teams_all.get(0).select("tbody > tr");
for (Element row : rows) {
Team team = new Team();
Elements columns = row.select("td");
team.setRank(Integer.valueOf(columns.get(1).text()));
team.setLeagueName(leagueName);
team.setTeamName(columns.get(3).select("a").text());
team.setScore(Integer.valueOf(columns.get(9).text()));

teamList.add(team);
}

/**主场*/
Elements teams_home = document.getElementsByClass("teams_home");
Elements home_rows = teams_home.get(0).select("tbody > tr");
for (Element row : home_rows) {
Elements columns = row.select("td");
String teamName = columns.get(3).select("a").text();
Team team = teamList.stream().filter(t -> t.getTeamName().equals(teamName) && t.getLeagueName().equals(leagueName)).findFirst().orElse(null);
team.setHomeNumber(Integer.valueOf(columns.get(4).text()));
team.setHomeWin(Integer.valueOf(columns.get(5).text()));
team.setHomeDraw(Integer.valueOf(columns.get(6).text()));
team.setHomeLost(Integer.valueOf(columns.get(7).text()));

BigDecimal total = new BigDecimal(columns.get(4).text());
BigDecimal gs = new BigDecimal(columns.get(8).text().split(":")[0].trim());
BigDecimal ga = new BigDecimal(columns.get(8).text().split(":")[1].trim());
BigDecimal homeAvgGS = total.compareTo(BigDecimal.ZERO) > 0 ? gs.divide(total, 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
BigDecimal homeAvgGA = total.compareTo(BigDecimal.ZERO) > 0 ? ga.divide(total, 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
team.setHomeAvgGS(homeAvgGS);
team.setHomeAvgGA(homeAvgGA);
}
/**客场*/
Elements teams_away = document.getElementsByClass("teams_away");
Elements away_rows = teams_away.get(0).select("tbody > tr");
for (Element row : away_rows) {
Elements columns = row.select("td");
String teamName = columns.get(3).select("a").text();
Team team = teamList.stream().filter(t -> t.getTeamName().equals(teamName) && t.getLeagueName().equals(leagueName)).findFirst().orElse(null);
team.setAwayNumber(Integer.valueOf(columns.get(4).text()));
team.setAwayWin(Integer.valueOf(columns.get(5).text()));
team.setAwayDraw(Integer.valueOf(columns.get(6).text()));
team.setAwayLost(Integer.valueOf(columns.get(7).text()));

BigDecimal total = new BigDecimal(columns.get(4).text());
BigDecimal gs = new BigDecimal(columns.get(8).text().split(":")[0].trim());
BigDecimal ga = new BigDecimal(columns.get(8).text().split(":")[1].trim());
BigDecimal awayAvgGS = total.compareTo(BigDecimal.ZERO) > 0 ? gs.divide(total, 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
BigDecimal awayAvgGA = total.compareTo(BigDecimal.ZERO) > 0 ? ga.divide(total, 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
team.setAwayAvgGS(awayAvgGS);
team.setAwayAvgGA(awayAvgGA);
}
return teamList;
}

public void insertTeamData(List<Team> teamList, String fileName, String sheetName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
XSSFSheet sheet = createMySheet(workbook, sheetName);
for(Team t : teamList) {
/** 记录不存在插入新的一条*/
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
row.createCell(0).setCellValue(t.getRank());
row.createCell(1).setCellValue(t.getLeagueName());
row.createCell(2).setCellValue(t.getTeamName());
row.createCell(3).setCellValue(t.getScore());

row.createCell(4).setCellValue(String.valueOf(t.getHomeNumber()));
row.createCell(5).setCellValue(t.getHomeWin() + "/" + t.getHomeDraw() + "/" + t.getHomeLost());
row.createCell(6).setCellValue(t.getHomeAvgGS().toString());
row.createCell(7).setCellValue(t.getHomeAvgGA().toString());

row.createCell(8).setCellValue(String.valueOf(t.getAwayNumber()));
row.createCell(9).setCellValue(t.getAwayWin() + "/" + t.getAwayDraw() + "/" + t.getAwayLost());
row.createCell(10).setCellValue(t.getAwayAvgGS().toString());
row.createCell(11).setCellValue(t.getAwayAvgGA().toString());

}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}

public XSSFSheet createMySheet(XSSFWorkbook workbook, String sheetName) throws IOException {
XSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
sheet = workbook.createSheet(sheetName);
sheet.setColumnWidth(0, 10*256);
sheet.setColumnWidth(1, 10*256);
sheet.setColumnWidth(2, 16*256);
sheet.setColumnWidth(3, 10*256);
sheet.setColumnWidth(4, 10*256);
sheet.setColumnWidth(5, 12*256);
sheet.setColumnWidth(6, 12*256);
sheet.setColumnWidth(7, 12*256);
sheet.setColumnWidth(8, 10*256);
sheet.setColumnWidth(9, 12*256);
sheet.setColumnWidth(10, 12*256);
sheet.setColumnWidth(11, 12*256);

XSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("排名");
row.createCell(1).setCellValue("联赛");
row.createCell(2).setCellValue("球队");
row.createCell(3).setCellValue("积分");
row.createCell(4).setCellValue("主场");
row.createCell(5).setCellValue("胜平负");
row.createCell(6).setCellValue("场均进球(主)");
row.createCell(7).setCellValue("场均失球(主)");
row.createCell(8).setCellValue("客场");
row.createCell(9).setCellValue("胜平负");
row.createCell(10).setCellValue("场均进球(客)");
row.createCell(11).setCellValue("场均失球(客)");

}
return sheet;
}

public void createCellStyle(String fileName, String sheetName) throws IOException {
Workbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
Sheet sheet = workbook.getSheet(sheetName);
Font font = workbook.createFont();
font.setFontName("Lucida Sans");
font.setFontHeightInPoints((short) 10);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
for (int i = 0; i < sheet.getLastRowNum() + 1 ; i++) {
Row row = sheet.getRow(i);
row.getCell(0).setCellStyle(cellStyle);
row.getCell(1).setCellStyle(cellStyle);
row.getCell(2).setCellStyle(cellStyle);
row.getCell(3).setCellStyle(cellStyle);
row.getCell(4).setCellStyle(cellStyle);
row.getCell(5).setCellStyle(cellStyle);
row.getCell(6).setCellStyle(cellStyle);
row.getCell(7).setCellStyle(cellStyle);
row.getCell(8).setCellStyle(cellStyle);
row.getCell(9).setCellStyle(cellStyle);
row.getCell(10).setCellStyle(cellStyle);
row.getCell(11).setCellStyle(cellStyle);
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}
}
posted @ 2025-07-11 09:21  狗狗听话  阅读(10)  评论(0)    收藏  举报