Browse Source

上传文件至 'project/command'

main
HuangZhikai 3 weeks ago
parent
commit
30800d0beb
  1. 46
      project/command/CrawlCommand.java

46
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());
}
}
}
Loading…
Cancel
Save