0

I am having trouble getting the following code to compile in groovy.

String execute(Document doc){

    CompilerConfiguration configuration = new CompilerConfiguration()
    configuration.setSourceEncoding("UTF-8")

    binding = new Binding();
    binding.setVariable("doc", doc)

    shell = new GroovyShell(binding, configuration)

    String clipping = shell.evaluate("doc."+jsoupExpression+".text()")

    return clipping

}

This is what should be executed when I call my function like this:

//Use a document from test/resources as input
Document doc = Jsoup.parse(new File("test/resources/online.html"), "UTF-8")

//This is what gets passed as jsoupExpression
Rule r = new Rule("select(div#unten div h2).get(1)")

String result = r.execute(doc)

What I get is this exception:

| Failure:  testExecute(com.threefact.scrapetastic.RuleTests)
|  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected char: '#' @ line 1, column 15.
   doc.select(div#unten div h2).get(1).text()
                 ^
1 error

I googled this exception for a while now, but cannot come up with how to resolve this issue. Perhaps someone has experienced a similar situation already and can help me out with this.

Thanks.

4

1 回答 1

1

The problem is this line:

Rule r = new Rule("select(div#unten div h2).get(1)")

When you isolate the rule, you get this program:

select(div#unten div h2).get(1)

I think you want to pass a string argument to select, so this would be the correct program:

select("div#unten div h2").get(1)

This means the Rule r line should be written as follows:

Rule r = new Rule("select(\"div#unten div h2\").get(1)")
于 2012-12-01T13:21:09.683 回答