|
@@ -0,0 +1,154 @@
|
|
|
|
|
+package space.anyi.app;
|
|
|
|
|
+
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
+import space.anyi.app.model.Institution;
|
|
|
|
|
+import space.anyi.app.model.Response;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.http.HttpClient;
|
|
|
|
|
+import java.net.http.HttpRequest;
|
|
|
|
|
+import java.net.http.HttpResponse;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @fileName: Application
|
|
|
|
|
+ * @projectName: data_acquisition
|
|
|
|
|
+ * @package: space.anyi.app
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @date:2026/5/1 22:06
|
|
|
|
|
+ * @description:
|
|
|
|
|
+ */
|
|
|
|
|
+public class Application {
|
|
|
|
|
+ private final static String templateUrl = "https://tuoyu.cpdrc.org.cn/bapfopm/pub/search/action/queryInfo?pageSize=%d&pageNum=%d";
|
|
|
|
|
+ private final static HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
|
|
+ private final static ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ int pageSize = 10;
|
|
|
|
|
+ int pageNum = 1;
|
|
|
|
|
+ Integer totalPage;
|
|
|
|
|
+ //初次获取数据,获取总数据条数
|
|
|
|
|
+ String url = templateUrl.formatted(pageSize, pageNum);
|
|
|
|
|
+ HttpRequest httpRequest = HttpRequest.newBuilder(URI.create(url)).GET().build();
|
|
|
|
|
+ try {
|
|
|
|
|
+ //发送请求
|
|
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
|
|
|
|
+ //获取响应数据
|
|
|
|
|
+ Response response = objectMapper.readValue(httpResponse.body(), Response.class);
|
|
|
|
|
+ totalPage = response.getResponseData().getTotalPage();
|
|
|
|
|
+ //批量处理
|
|
|
|
|
+ handler(totalPage, pageSize);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void handler(Integer totalPage, int pageSize) {
|
|
|
|
|
+ List<Institution> res = new ArrayList<>();
|
|
|
|
|
+ System.out.println("开始爬取数据,共 " + totalPage + " 页...");
|
|
|
|
|
+ for (int i = 1; i <= totalPage; i++) {
|
|
|
|
|
+ String url = templateUrl.formatted(pageSize, i);
|
|
|
|
|
+ HttpRequest httpRequest = HttpRequest.newBuilder(URI.create(url)).GET().build();
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
|
|
|
|
+ Response response = objectMapper.readValue(httpResponse.body(), Response.class);
|
|
|
|
|
+ //处理数据
|
|
|
|
|
+ res.addAll(response.getResponseData().getDataList());
|
|
|
|
|
+ //打印进度
|
|
|
|
|
+ System.out.printf("\r进度: [%d/%d] 页, 已获取 %d 条记录", i, totalPage, res.size());
|
|
|
|
|
+ //休眠1秒
|
|
|
|
|
+ //Thread.sleep(100L);
|
|
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ System.out.println(); // 换行
|
|
|
|
|
+ saveToExcel(res, "data.xlsx");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void save(List<Institution> list) {
|
|
|
|
|
+ //模拟数据储存
|
|
|
|
|
+ for (Institution institution : list) {
|
|
|
|
|
+ System.out.println(institution);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void saveToExcel(List<Institution> institutions, String filePath) {
|
|
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
|
|
+ Sheet sheet = workbook.createSheet("托育机构信息");
|
|
|
|
|
+
|
|
|
|
|
+ // 创建标题行样式
|
|
|
|
|
+ CellStyle headerStyle = workbook.createCellStyle();
|
|
|
|
|
+ Font headerFont = workbook.createFont();
|
|
|
|
|
+ headerFont.setBold(true);
|
|
|
|
|
+ headerStyle.setFont(headerFont);
|
|
|
|
|
+ headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
|
|
+ headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
+ headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
+ headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
+ headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
+ headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建内容行样式
|
|
|
|
|
+ CellStyle contentStyle = workbook.createCellStyle();
|
|
|
|
|
+ contentStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
+ contentStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
+ contentStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
+ contentStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建表头
|
|
|
|
|
+ String[] headers = {"机构名称", "机构别名", "机构类型", "信用代码", "地址",
|
|
|
|
|
+ "所在区域", "区域代码", "完成时间"};
|
|
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
+ Cell cell = headerRow.createCell(i);
|
|
|
|
|
+ cell.setCellValue(headers[i]);
|
|
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
|
|
+ sheet.setColumnWidth(i, 5000);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 填充数据
|
|
|
|
|
+ int rowNum = 1;
|
|
|
|
|
+ for (Institution inst : institutions) {
|
|
|
|
|
+ Row row = sheet.createRow(rowNum++);
|
|
|
|
|
+
|
|
|
|
|
+ row.createCell(0).setCellValue(inst.getInstitutionName());
|
|
|
|
|
+ row.createCell(1).setCellValue(inst.getInstitutionOtherName());
|
|
|
|
|
+ row.createCell(2).setCellValue(inst.getInstitutionType());
|
|
|
|
|
+ row.createCell(3).setCellValue(inst.getCreditCode());
|
|
|
|
|
+ row.createCell(4).setCellValue(inst.getAddress());
|
|
|
|
|
+ row.createCell(5).setCellValue(inst.getZoningName());
|
|
|
|
|
+ row.createCell(6).setCellValue(inst.getZoningCode());
|
|
|
|
|
+ row.createCell(7).setCellValue(inst.getFinishedTime());
|
|
|
|
|
+
|
|
|
|
|
+ // 应用样式
|
|
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
+ row.getCell(i).setCellStyle(contentStyle);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 写入文件
|
|
|
|
|
+ try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
|
|
|
|
|
+ workbook.write(fileOut);
|
|
|
|
|
+ System.out.println("\n数据已成功保存到Excel文件: " + filePath);
|
|
|
|
|
+ System.out.println("共保存 " + institutions.size() + " 条记录");
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ System.err.println("保存Excel文件失败: " + e.getMessage());
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ workbook.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|