Files
ljwx-bookstore/SimpleLocalHtmlTest.java
bruno 3bd6ad0cca
CI/CD Pipeline / test-build (push) Successful in 2m3s
CI/CD Pipeline / build-push (push) Failing after 0s
CI/CD Pipeline / update-deploy (push) Has been skipped
CI/CD Pipeline / build-pr (push) Has been skipped
commit to gitea
2026-01-16 09:15:03 +08:00

103 lines
4.3 KiB
Java

import cn.book.bus.service.crawler.LocalHtmlCrawlerService;
import cn.book.bus.domain.CrawlSourceConfig;
import cn.book.bus.domain.Fiction;
import cn.book.bus.dto.crawler.ChapterData;
import java.util.List;
import java.util.Map;
/**
* 简化的本地HTML解析测试
* 不依赖Spring框架,直接测试核心功能
*/
public class SimpleLocalHtmlTest {
public static void main(String[] args) {
System.out.println("=== 简化版本地HTML解析测试 ===");
// 创建服务实例
LocalHtmlCrawlerService localHtmlCrawlerService = new LocalHtmlCrawlerService();
// 检查本地HTML文件是否存在
boolean hasFiles = localHtmlCrawlerService.hasLocalHtmlFiles();
System.out.println("本地HTML文件存在: " + hasFiles);
if (!hasFiles) {
System.out.println("❌ 未找到本地HTML文件,请确保 html/ 目录下有 czbooks_catalog.html 和 czbooks_chapter.html 文件");
return;
}
// 创建测试小说对象
Fiction fiction = new Fiction();
fiction.setId(1);
fiction.setFictionName("我的老婆是頂流天后");
// 创建测试源配置
CrawlSourceConfig source = new CrawlSourceConfig();
source.setSourceName("local_html");
source.setRequiresLocalHtml(true);
source.setSelectors(Map.of(
"title", "span.title",
"author", "span.author a",
"intro", "div.description",
"chapterList", "ul#chapter-list li a",
"chapterTitle", "div.name",
"chapterContent", "div.content"
));
System.out.println("开始解析本地HTML文件...");
// 执行解析
List<ChapterData> chapters = localHtmlCrawlerService.parseFromLocalHtml(fiction, source);
if (chapters != null && !chapters.isEmpty()) {
System.out.println("✅ 解析成功!");
System.out.println("总章节数: " + chapters.size());
// 显示前几章的信息
int displayCount = Math.min(10, chapters.size()); // 只显示前10章
for (int i = 0; i < displayCount; i++) {
ChapterData chapter = chapters.get(i);
System.out.println((i + 1) + ". " + chapter.getTitle());
if (chapter.getTextContent() != null && !chapter.getTextContent().isEmpty()) {
System.out.println(" 内容长度: " + chapter.getTextContent().length() + " 字符");
// 显示内容的前100个字符
String preview = chapter.getTextContent().substring(0,
Math.min(100, chapter.getTextContent().length()));
System.out.println(" 预览: " + preview + "...");
} else {
System.out.println(" 内容: (暂无具体章节文件)");
}
System.out.println();
}
if (chapters.size() > displayCount) {
System.out.println("... 还有 " + (chapters.size() - displayCount) + " 章未显示");
}
// 统计有内容的章节数
long chaptersWithContent = chapters.stream()
.filter(c -> c.getTextContent() != null && !c.getTextContent().isEmpty())
.count();
System.out.println("有内容的章节数: " + chaptersWithContent);
// 显示一些统计信息
System.out.println("\n=== 统计信息 ===");
System.out.println("小说名称: " + fiction.getFictionName());
System.out.println("总章节数: " + chapters.size());
System.out.println("有内容章节数: " + chaptersWithContent);
if (chaptersWithContent > 0) {
int totalWords = chapters.stream()
.filter(c -> c.getTextContent() != null)
.mapToInt(c -> c.getTextContent().length())
.sum();
System.out.println("总字数: " + totalWords);
}
} else {
System.out.println("❌ 解析失败或没有找到章节");
}
System.out.println("=== 测试完成 ===");
}
}