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.
46 lines
1.3 KiB
46 lines
1.3 KiB
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());
|
|
}
|
|
}
|
|
}
|