문제

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.

도움이 되었습니까?

해결책

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)")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top