1 changed files with 53 additions and 0 deletions
@ -0,0 +1,53 @@ |
|||||
|
package org.example.service; |
||||
|
|
||||
|
import org.example.exception.CrawlerException; |
||||
|
import org.example.model.Article; |
||||
|
import org.example.strategy.BaiduStrategy; |
||||
|
import org.example.strategy.BingStrategy; |
||||
|
import org.example.strategy.CrawlerStrategy; |
||||
|
import org.example.strategy.CsdnStrategy; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class CrawlerService { |
||||
|
|
||||
|
/** |
||||
|
* 聚合搜索:同时调用所有策略进行搜索 |
||||
|
*/ |
||||
|
public List<Article> search(String keyword) throws CrawlerException { |
||||
|
if (keyword == null || keyword.trim().isEmpty()) { |
||||
|
throw new CrawlerException("关键词不能为空!"); |
||||
|
} |
||||
|
|
||||
|
System.out.println("Service层: 正在调度所有爬虫策略..."); |
||||
|
|
||||
|
List<CrawlerStrategy> strategies = new ArrayList<>(); |
||||
|
strategies.add(new BaiduStrategy()); |
||||
|
strategies.add(new BingStrategy()); |
||||
|
strategies.add(new CsdnStrategy()); |
||||
|
|
||||
|
List<Article> allResults = new ArrayList<>(); |
||||
|
|
||||
|
for (CrawlerStrategy strategy : strategies) { |
||||
|
try { |
||||
|
System.out.println("正在爬取 [" + strategy.getName() + "] ..."); |
||||
|
|
||||
|
// 调用具体策略的 crawl 方法
|
||||
|
List<Article> results = strategy.crawl(keyword); |
||||
|
|
||||
|
// 将当前策略的结果添加到总列表中
|
||||
|
allResults.addAll(results); |
||||
|
|
||||
|
System.out.println("[" + strategy.getName() + "] 爬取完成,获取到 " + results.size() + " 条数据。"); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
// 捕获单个策略的异常,防止因为一个网站挂了导致整个程序崩溃
|
||||
|
System.err.println("警告: [" + strategy.getName() + "] 爬取失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
System.out.println("搜索结束,共获取到 " + allResults.size() + " 条结果。"); |
||||
|
return allResults; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue