我有一个XML配置(ScreenScraper),可以在WebHarvest的可执行版本中正确执行我想要的操作。我对如何通过Java执行它感到困惑。
答案 0 :(得分:1)
您只需从库中导入一些类:
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.Variable;
使用config.xml文件创建对象ScraperConfiguration:
ScraperConfiguration config = null;
try {
config = new ScraperConfiguration("/path/to/config.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
创建具有工作目录路径的对象Scraper:
Scraper scraper = new Scraper(config, "/tmp/");
并执行配置:
scraper.execute();
您还可以在配置执行后访问变量:
String stringVar =
((Variable)scraper.getContext().getVar("my_string_var")).toString();
List<Variable> listVar =
((Variable) scraper.getContext().getVar("my_list_var")).toList();