diff --git a/bocai.db b/bocai.db index 05bb215..6ddebec 100644 Binary files a/bocai.db and b/bocai.db differ diff --git a/src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java b/src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java index 92f59a1..7d40ee7 100644 --- a/src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java +++ b/src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java @@ -26,7 +26,7 @@ public class CrawlerSchedule { // 每天凌晨2点执行爬取开奖结果 //@Scheduled(cron = "0 0 2 * * ?") // 每7秒执行一次爬取开奖结果 - //@Scheduled(cron = "*/9 * * * * ?") + @Scheduled(cron = "*/9 * * * * ?") public void executeLotteryDraw() { System.out.println("开始爬取开奖结果..."); int retryCount = 0; diff --git a/src/main/java/com/tem/bocai/util/LotteryWebMagicCrawler.java b/src/main/java/com/tem/bocai/util/LotteryWebMagicCrawler.java index 79e443c..db16633 100644 --- a/src/main/java/com/tem/bocai/util/LotteryWebMagicCrawler.java +++ b/src/main/java/com/tem/bocai/util/LotteryWebMagicCrawler.java @@ -12,6 +12,7 @@ import us.codecraft.webmagic.processor.PageProcessor; import us.codecraft.webmagic.selector.Html; import us.codecraft.webmagic.selector.Selectable; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -238,7 +239,7 @@ public class LotteryWebMagicCrawler implements PageProcessor { return resultList; } - public static void writeToJsonFile(List> resultList) { + public static void writeToJsonFile(List> resultList) { try { // 创建 ObjectMapper 实例 ObjectMapper objectMapper = new ObjectMapper(); @@ -246,9 +247,13 @@ public class LotteryWebMagicCrawler implements PageProcessor { // 设置 JSON 格式化(可选,更易读) objectMapper.enable(SerializationFeature.INDENT_OUTPUT); - // 定义输出目录和文件名 + // 定义输出目录 String directoryPath = "output/json"; // 项目根目录下的 output/json 文件夹 - String fileName = "result_" + System.currentTimeMillis() + ".json"; + + // 使用年月日作为文件名(格式:result_yyyyMMdd.json) + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); + String dateStr = dateFormat.format(new Date()); + String fileName = "result_" + dateStr + ".json"; String filePath = directoryPath + "/" + fileName; // 创建目录(如果不存在) @@ -256,8 +261,19 @@ public class LotteryWebMagicCrawler implements PageProcessor { if (!directory.exists()) { directory.mkdirs(); // 创建多级目录 } + // 创建文件对象 File outputFile = new File(filePath); + + // 如果文件已存在,删除旧文件(实现替换功能) + if (outputFile.exists()) { + boolean deleted = outputFile.delete(); + if (!deleted) { + throw new IOException("无法删除已存在的文件: " + filePath); + } + System.out.println("已删除旧文件,准备创建新文件: " + fileName); + } + // 将 List 写入 JSON 文件 objectMapper.writeValue(outputFile, resultList); System.out.println("数据已成功写入文件: " + outputFile.getAbsolutePath());