1 changed files with 238 additions and 0 deletions
@ -0,0 +1,238 @@ |
|||||
|
import java.util.*; |
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* Article类 - 表示爬虫抓取的文章 |
||||
|
*/ |
||||
|
class Article { |
||||
|
private String title; |
||||
|
private String content; |
||||
|
private String author; // 新增字段:作者
|
||||
|
private Date publishDate; // 新增字段:发布时间
|
||||
|
|
||||
|
public Article(String title, String content) { |
||||
|
this.title = title; |
||||
|
this.content = content; |
||||
|
this.author = null; // 默认值为null
|
||||
|
this.publishDate = null; // 默认值为null
|
||||
|
} |
||||
|
|
||||
|
public Article(String title, String content, String author, Date publishDate) { |
||||
|
this.title = title; |
||||
|
this.content = content; |
||||
|
this.author = author; |
||||
|
this.publishDate = publishDate; |
||||
|
} |
||||
|
|
||||
|
// Getter 和 Setter 方法
|
||||
|
public String getTitle() { return title; } |
||||
|
public void setTitle(String title) { this.title = title; } |
||||
|
|
||||
|
public String getContent() { return content; } |
||||
|
public void setContent(String content) { this.content = content; } |
||||
|
|
||||
|
public String getAuthor() { return author; } |
||||
|
public void setAuthor(String author) { this.author = author; } |
||||
|
|
||||
|
public Date getPublishDate() { return publishDate; } |
||||
|
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
String dateStr = (publishDate != null) ? publishDate.toString() : "未知"; |
||||
|
String authorStr = (author != null && !author.trim().isEmpty()) ? author : "未知"; |
||||
|
return "标题: " + title + ", 作者: " + authorStr + ", 发布时间: " + dateStr; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* HistoryCommand类 - 记录命令历史 |
||||
|
*/ |
||||
|
class HistoryCommand { |
||||
|
private List<String> historyList = new ArrayList<>(); |
||||
|
|
||||
|
// 添加命令到历史记录
|
||||
|
public void addCommand(String command) { |
||||
|
historyList.add(command); |
||||
|
} |
||||
|
|
||||
|
// 获取所有历史记录
|
||||
|
public List<String> getHistory() { |
||||
|
return new ArrayList<>(historyList); // 返回副本,防止外部修改
|
||||
|
} |
||||
|
|
||||
|
// 获取最近N条历史记录
|
||||
|
public List<String> getRecentHistory(int n) { |
||||
|
if (n <= 0) return new ArrayList<>(); |
||||
|
int startIndex = Math.max(0, historyList.size() - n); |
||||
|
return new ArrayList<>(historyList.subList(startIndex, historyList.size())); |
||||
|
} |
||||
|
|
||||
|
// 清空历史记录
|
||||
|
public void clearHistory() { |
||||
|
historyList.clear(); |
||||
|
} |
||||
|
|
||||
|
// 获取历史记录数量
|
||||
|
public int getHistoryCount() { |
||||
|
return historyList.size(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 主程序 - 演示类功能 |
||||
|
*/ |
||||
|
public class CrawlerApp { |
||||
|
// 模拟文章列表
|
||||
|
private static List<Article> articleList = new ArrayList<>(); |
||||
|
private static HistoryCommand history = new HistoryCommand(); |
||||
|
private static Scanner scanner = new Scanner(System.in); |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("=== 爬虫系统控制台 ==="); |
||||
|
System.out.println("命令列表:"); |
||||
|
System.out.println(" crawl - 开始抓取文章"); |
||||
|
System.out.println(" list - 显示已抓取的文章"); |
||||
|
System.out.println(" add - 手动添加文章"); |
||||
|
System.out.println(" history - 查看命令历史"); |
||||
|
System.out.println(" clear - 清空命令历史"); |
||||
|
System.out.println(" exit - 退出系统"); |
||||
|
System.out.println("========================\n"); |
||||
|
|
||||
|
// 添加一些示例文章
|
||||
|
articleList.add(new Article("Java编程入门", "Java是一种面向对象的编程语言...", "张三", new Date())); |
||||
|
articleList.add(new Article("Python数据分析", "使用Python进行数据清洗和分析...", "李四", new Date())); |
||||
|
|
||||
|
boolean running = true; |
||||
|
while (running) { |
||||
|
System.out.print("> "); |
||||
|
String input = scanner.nextLine().trim(); |
||||
|
|
||||
|
if (input.isEmpty()) continue; |
||||
|
|
||||
|
// 记录命令历史
|
||||
|
history.addCommand(input); |
||||
|
|
||||
|
// 解析命令
|
||||
|
String[] parts = input.split("\\s+"); |
||||
|
String command = parts[0].toLowerCase(); |
||||
|
|
||||
|
switch (command) { |
||||
|
case "crawl": |
||||
|
handleCrawlCommand(parts); |
||||
|
break; |
||||
|
case "list": |
||||
|
handleListCommand(); |
||||
|
break; |
||||
|
case "add": |
||||
|
handleAddCommand(); |
||||
|
break; |
||||
|
case "history": |
||||
|
handleHistoryCommand(parts); |
||||
|
break; |
||||
|
case "clear": |
||||
|
history.clearHistory(); |
||||
|
System.out.println("命令历史已清空"); |
||||
|
break; |
||||
|
case "exit": |
||||
|
System.out.println("退出系统..."); |
||||
|
running = false; |
||||
|
break; |
||||
|
default: |
||||
|
System.out.println("未知命令: " + command); |
||||
|
System.out.println("输入 'exit' 退出系统"); |
||||
|
} |
||||
|
} |
||||
|
scanner.close(); |
||||
|
} |
||||
|
|
||||
|
// 处理爬取命令
|
||||
|
private static void handleCrawlCommand(String[] parts) { |
||||
|
String keyword = (parts.length > 1) ? parts[1] : "新闻"; |
||||
|
System.out.println("正在抓取关于 '" + keyword + "' 的文章..."); |
||||
|
|
||||
|
// 模拟抓取过程
|
||||
|
try { |
||||
|
Thread.sleep(1000); |
||||
|
} catch (InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
// 模拟抓取结果
|
||||
|
Article newArticle = new Article( |
||||
|
"关于" + keyword + "的最新报道", |
||||
|
"这是关于" + keyword + "的详细内容...", |
||||
|
"系统抓取", |
||||
|
new Date() |
||||
|
); |
||||
|
|
||||
|
articleList.add(newArticle); |
||||
|
System.out.println("抓取完成!新增文章: " + newArticle.getTitle()); |
||||
|
} |
||||
|
|
||||
|
// 处理列出文章命令
|
||||
|
private static void handleListCommand() { |
||||
|
if (articleList.isEmpty()) { |
||||
|
System.out.println("当前没有文章"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
System.out.println("\n=== 已抓取的文章列表 ==="); |
||||
|
for (int i = 0; i < articleList.size(); i++) { |
||||
|
System.out.println((i + 1) + ". " + articleList.get(i)); |
||||
|
} |
||||
|
System.out.println("总共 " + articleList.size() + " 篇文章\n"); |
||||
|
} |
||||
|
|
||||
|
// 处理添加文章命令
|
||||
|
private static void handleAddCommand() { |
||||
|
System.out.println("\n--- 添加新文章 ---"); |
||||
|
|
||||
|
System.out.print("请输入文章标题: "); |
||||
|
String title = scanner.nextLine().trim(); |
||||
|
|
||||
|
System.out.print("请输入文章内容: "); |
||||
|
String content = scanner.nextLine().trim(); |
||||
|
|
||||
|
System.out.print("请输入作者(可选,直接回车跳过): "); |
||||
|
String author = scanner.nextLine().trim(); |
||||
|
if (author.isEmpty()) author = null; |
||||
|
|
||||
|
System.out.print("请输入发布日期(yyyy-MM-dd,可选,直接回车跳过): "); |
||||
|
String dateStr = scanner.nextLine().trim(); |
||||
|
Date publishDate = null; |
||||
|
if (!dateStr.isEmpty()) { |
||||
|
try { |
||||
|
// 简单日期解析,实际项目中应该使用SimpleDateFormat
|
||||
|
publishDate = new Date(); |
||||
|
} catch (Exception e) { |
||||
|
System.out.println("日期格式错误,使用当前日期"); |
||||
|
publishDate = new Date(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Article newArticle = new Article(title, content, author, publishDate); |
||||
|
articleList.add(newArticle); |
||||
|
System.out.println("文章添加成功!\n"); |
||||
|
} |
||||
|
|
||||
|
// 处理历史命令
|
||||
|
private static void handleHistoryCommand(String[] parts) { |
||||
|
if (parts.length > 1 && parts[1].equals("count")) { |
||||
|
System.out.println("历史命令总数: " + history.getHistoryCount()); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
List<String> historyList = history.getHistory(); |
||||
|
if (historyList.isEmpty()) { |
||||
|
System.out.println("命令历史为空"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
System.out.println("\n=== 命令历史 ==="); |
||||
|
for (int i = 0; i < historyList.size(); i++) { |
||||
|
System.out.println((i + 1) + ". " + historyList.get(i)); |
||||
|
} |
||||
|
System.out.println(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue