From 30800d0beb4155d7559769b6be0b455e6a1c8dcb Mon Sep 17 00:00:00 2001 From: HuangZhikai <386754646@qq.com> Date: Sun, 31 May 2026 14:47:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'project/command'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/command/CrawlCommand.java | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 project/command/CrawlCommand.java diff --git a/project/command/CrawlCommand.java b/project/command/CrawlCommand.java new file mode 100644 index 0000000..6540088 --- /dev/null +++ b/project/command/CrawlCommand.java @@ -0,0 +1,46 @@ +package com.crawler.command; + +import com.crawler.controller.CrawlerController; +import com.crawler.crawler.Crawler; +import com.crawler.crawler.CrawlerFactory; +import com.crawler.model.CrawlerConfig; + +public class CrawlCommand extends BaseCommand { + @Override + public String getName() { + return "crawl"; + } + + @Override + public String getDescription() { + return "运行爬虫,输入URL自动选择爬虫"; + } + + @Override + public void execute() { + String url = readInput("请输入要爬取的URL: "); + + if (url.isEmpty()) { + System.out.println("错误: URL不能为空"); + return; + } + + try { + CrawlerFactory factory = CrawlerFactory.getInstance(); + Crawler crawler = factory.createCrawler(url); + String crawlerName = factory.getCrawlerName(url); + + System.out.println("使用爬虫: " + crawlerName); + + CrawlerConfig config = new CrawlerConfig(); + config.setTargetUrl(url); + + CrawlerController controller = new CrawlerController(crawler); + controller.setConfig(config); + controller.execute(); + + } catch (Exception e) { + System.err.println("爬虫执行失败: " + e.getMessage()); + } + } +} \ No newline at end of file