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.
35 lines
1.0 KiB
35 lines
1.0 KiB
package com.crawler.common;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public abstract class BaseCrawler<T> {
|
|
|
|
public abstract List<T> crawl();
|
|
|
|
public void saveToCSV(List<T> data, String filePath) throws IOException {
|
|
if (data == null || data.isEmpty()) {
|
|
LoggerUtil.warn("没有数据需要保存");
|
|
return;
|
|
}
|
|
|
|
CsvUtil.ensureDirectoryExists(filePath);
|
|
List<String[]> csvData = convertToCsvFormat(data);
|
|
CsvUtil.write(filePath, csvData);
|
|
}
|
|
|
|
public List<T> loadFromCSV(String filePath) throws IOException {
|
|
if (!CsvUtil.fileExists(filePath)) {
|
|
LoggerUtil.warn("文件不存在: {}", filePath);
|
|
return new ArrayList<>();
|
|
}
|
|
|
|
List<String[]> csvData = CsvUtil.read(filePath);
|
|
return convertFromCsvFormat(csvData);
|
|
}
|
|
|
|
protected abstract List<String[]> convertToCsvFormat(List<T> data);
|
|
|
|
protected abstract List<T> convertFromCsvFormat(List<String[]> csvData);
|
|
}
|