0

次のコードを 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

}

これは、次のように関数を呼び出すときに実行する必要があるものです。

//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)

私が得るのはこの例外です:

| 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

この例外をしばらくグーグルで検索しましたが、この問題を解決する方法がわかりません。おそらく、誰かがすでに同様の状況を経験しており、これで私を助けることができます.

ありがとう。

4

1 に答える 1

1

問題はこの行です:

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

ルールを分離すると、次のプログラムが表示されます。

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

文字列引数をに渡したいと思うselectので、これは正しいプログラムになります。

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

これは、Rule r行を次のように記述する必要があることを意味します。

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