Browse Source

上传文件至 '新闻网爬虫'

main
peisishuang 2 months ago
parent
commit
551a9586b8
  1. 59
      新闻网爬虫/Article.java
  2. 43
      新闻网爬虫/CommandController.java
  3. 41
      新闻网爬虫/ConsoleView.java
  4. 9
      新闻网爬虫/OutputView.java

59
新闻网爬虫/Article.java

@ -0,0 +1,59 @@
package com.cctv.news.model;
public class Article {
private String title;
private String url;
private String publishTime;
private String content;
public Article() {
}
public Article(String title, String url, String publishTime, String content) {
this.title = title;
this.url = url;
this.publishTime = publishTime;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article{" +
"title='" + title + '\'' +
", url='" + url + '\'' +
", publishTime='" + publishTime + '\'' +
'}';
}
}

43
新闻网爬虫/CommandController.java

@ -0,0 +1,43 @@
package com.cctv.news.controller;
import com.cctv.news.command.Command;
import com.cctv.news.view.OutputView;
import java.util.HashMap;
import java.util.Map;
public class CommandController {
private final Map<String, Command> commands;
private final OutputView view;
public CommandController(OutputView view) {
this.view = view;
this.commands = new HashMap<>();
}
public void registerCommand(Command command) {
commands.put(command.getName(), command);
}
public void executeCommand(String input) {
if (input == null || input.trim().isEmpty()) {
view.showError("命令不能为空");
return;
}
String[] parts = input.trim().split("\\s+");
String commandName = parts[0];
String[] args = parts.length > 1 ? java.util.Arrays.copyOfRange(parts, 1, parts.length) : new String[0];
Command command = commands.get(commandName);
if (command != null) {
try {
command.execute(args);
} catch (Exception e) {
view.showError("执行命令时出错: " + e.getMessage());
}
} else {
view.showError("未知命令: " + commandName + ",输入 help 查看可用命令");
}
}
}

41
新闻网爬虫/ConsoleView.java

@ -0,0 +1,41 @@
package com.cctv.news.view;
public class ConsoleView implements OutputView {
private static final String ANSI_RESET = "\033[0m";
private static final String ANSI_CYAN = "\033[36m";
private static final String ANSI_RED = "\033[31m";
private static final String ANSI_GREEN = "\033[32m";
private static final String ANSI_MAGENTA = "\033[35m";
private static final String ANSI_YELLOW = "\033[33m";
private static final String PREFIX_INFO = ANSI_CYAN + "[INFO]" + ANSI_RESET + " ";
private static final String PREFIX_ERROR = ANSI_RED + "[ERROR]" + ANSI_RESET + " ";
private static final String PREFIX_ARTICLE = ANSI_GREEN + "[ARTICLE]" + ANSI_RESET + " ";
@Override
public void showMessage(String message) {
System.out.println(PREFIX_INFO + message);
}
@Override
public void showError(String error) {
System.out.println(PREFIX_ERROR + error);
}
@Override
public void showArticles(String articles) {
System.out.println(PREFIX_ARTICLE + articles);
}
@Override
public void showWelcome() {
System.out.println(ANSI_MAGENTA + "========================================" + ANSI_RESET);
System.out.println(ANSI_MAGENTA + " 央视新闻爬虫 v1.0" + ANSI_RESET);
System.out.println(ANSI_MAGENTA + "========================================" + ANSI_RESET);
}
@Override
public void showPrompt() {
System.out.print(ANSI_YELLOW + "请输入命令>" + ANSI_RESET + " ");
}
}

9
新闻网爬虫/OutputView.java

@ -0,0 +1,9 @@
package com.cctv.news.view;
public interface OutputView {
void showMessage(String message);
void showError(String error);
void showArticles(String articles);
void showWelcome();
void showPrompt();
}
Loading…
Cancel
Save