eclipse中的webharvest实现

时间:2012-07-26 20:53:47

标签: xml screen-scraping webharvest

我有一个XML配置(ScreenScraper),可以在WebHarvest的可执行版本中正确执行我想要的操作。我对如何通过Java执行它感到困惑。

1 个答案:

答案 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();

You can see example here

And also API here