gpt4 book ai didi

compiler-errors - 如何使Groovy Shell读取#符号

转载 作者:行者123 更新时间:2023-12-02 10:55:19 25 4
gpt4 key购买 nike

我无法让以下代码在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)

我得到的是这个异常(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

我用谷歌搜索了一段时间,但无法提出解决方案。也许有人已经经历过类似的情况,可以帮助我解决这个问题。

谢谢。

最佳答案

问题是这一行:

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

关于compiler-errors - 如何使Groovy Shell读取#符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13659563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com