我无法让以下代码在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
最佳答案
问题是这一行:
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)")
https://stackoverflow.com/questions/13659563/