Skip to content

Commit

Permalink
🐛 修复某些情况下搜索失败
Browse files Browse the repository at this point in the history
  • Loading branch information
freeok committed Dec 5, 2024
1 parent 6070462 commit ed76577
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
22 changes: 10 additions & 12 deletions src/main/java/com/pcdd/sonovel/parse/SearchResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jsoup.select.Elements;

import java.util.*;
import java.util.stream.Stream;

/**
* @author pcdd
Expand Down Expand Up @@ -67,24 +66,23 @@ public List<SearchResult> parse(String keyword) {

@SneakyThrows
private List<SearchResult> getSearchResults(String url, Document document) {
Rule.Search search = this.rule.getSearch();
Rule.Search rule = this.rule.getSearch();
// 搜索结果页 DOM
if (document == null)
document = Jsoup.connect(url).timeout(TIMEOUT_MILLS).get();

Elements elements = document.select(search.getResult());

Elements elements = document.select(rule.getResult());
List<SearchResult> list = new ArrayList<>();
for (Element element : elements) {
// jsoup 不支持一次性获取属性的值
String href = element.select(search.getBookName()).attr("href");
String bookName = element.select(search.getBookName()).text();
String latestChapter = element.select(search.getLatestChapter()).text();
String author = element.select(search.getAuthor()).text();
String update = element.select(search.getUpdate()).text();
String href = element.select(rule.getBookName()).attr("href");
String bookName = element.select(rule.getBookName()).text();
String latestChapter = element.select(rule.getLatestChapter()).text();
String author = element.select(rule.getAuthor()).text();
String update = element.select(rule.getUpdate()).text();

// 如果存在任何一个字符串为空字符串(针对书源 1:排除第一个 tr 表头
if (Stream.of(href, bookName, latestChapter, author, update).anyMatch(String::isEmpty)) continue;
// 针对书源 1:排除第一个 tr 表头
if (bookName.isEmpty()) continue;

SearchResult build = SearchResult.builder()
.url(CrawlUtils.normalizeUrl(href, this.rule.getUrl()))
Expand All @@ -100,4 +98,4 @@ private List<SearchResult> getSearchResults(String url, Document document) {
return list;
}

}
}
8 changes: 3 additions & 5 deletions src/test/java/com/pcdd/sonovel/BookSourceQualityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void generateMarkdown(String name, List<List<SourceQuality>> lists, Strin
name,
lists.get(0).size(),
DateTime.now().toString(DatePattern.NORM_DATE_PATTERN)));
result.append(StrUtil.format("| 序号 | 书名 | 作者 {} 起点链接 |\n", s1));
result.append(StrUtil.format("| 排名 | 书名 | 作者 {} 起点链接 |\n", s1));
result.append(StrUtil.format("| ---- | ---- | ---- {} ---- |\n", s2));

List<SourceQuality> list = lists.get(0);
Expand All @@ -87,14 +87,12 @@ static void generateMarkdown(String name, List<List<SourceQuality>> lists, Strin
foundBuilder.append(StrUtil.format("{} |", sq.getFound() ? "✅" : "❌"));
}

String s = StrUtil.format("| {} | {} | {} | {} {} |\n",
result.append(StrUtil.format("| {} | {} | {} | {} {} |\n",
i + 1,
o.getBookName(),
o.getAuthor(),
foundBuilder,
o.getQiDianUrl());

result.append(s);
o.getQiDianUrl()));
}

Writer writer = new FileWriter(fileName);
Expand Down

0 comments on commit ed76577

Please sign in to comment.