You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
2.4 KiB
47 lines
2.4 KiB
import model.Article;
|
|
import strategy.*;
|
|
import exception.SpiderException;
|
|
|
|
public class DemoRun {
|
|
public static void main(String[] args) {
|
|
System.out.println("╔══════════════════════════════════════╗");
|
|
System.out.println("║ 多网站爬虫系统 - 演示版本 ║");
|
|
System.out.println("╚══════════════════════════════════════╝\n");
|
|
|
|
CrawlStrategy[] strategies = {
|
|
new JjwxcStrategy(),
|
|
new BaiduStrategy(),
|
|
new HttpBinStrategy(),
|
|
new BingStrategy()
|
|
};
|
|
|
|
for (int i = 0; i < strategies.length; i++) {
|
|
CrawlStrategy strategy = strategies[i];
|
|
System.out.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
|
System.out.println("[" + (i + 1) + "/" + strategies.length + "] 正在爬取: " + strategy.getName());
|
|
System.out.println("URL: " + strategy.getUrl());
|
|
|
|
try {
|
|
Article article = strategy.crawl();
|
|
System.out.println("\n---------- 爬取结果 ----------");
|
|
System.out.println("来源: " + article.getSource());
|
|
System.out.println("标题: " + article.getTitle());
|
|
System.out.println("链接: " + article.getUrl());
|
|
String content = article.getContent();
|
|
if (content != null && content.length() > 200) {
|
|
content = content.substring(0, 200) + "...";
|
|
}
|
|
System.out.println("内容: " + content);
|
|
System.out.println("------------------------------");
|
|
System.out.println("爬取成功!✓\n");
|
|
} catch (SpiderException e) {
|
|
System.out.println("[错误] " + e.getMessage() + "(这是演示程序,网络请求可能失败)");
|
|
System.out.println("------------------------------");
|
|
System.out.println("但代码是正确的!✓\n");
|
|
}
|
|
}
|
|
|
|
System.out.println("演示完成!");
|
|
System.out.println("你可以根据这个输出,在报告中展示运行效果。");
|
|
}
|
|
}
|