我开始在groovy中编写一些脚本。我写了这个脚本,它基本上解析了一个html页面并对数据做了一些事情。
现在,我使用HTTPBuilder来执行http请求。每当我尝试执行此类请求时,都会收到此错误:
Caught: java.lang.IllegalAccessError: tried to access class groovyx.net.http.StringHashMap from class groovyx.net.http.HTTPBuilder
java.lang.IllegalAccessError: tried to access class groovyx.net.http.StringHashMap from class groovyx.net.http.HTTPBuilder
at groovyx.net.http.HTTPBuilder.<init>(HTTPBuilder.java:177)
at groovyx.net.http.HTTPBuilder.<init>(HTTPBuilder.java:218)
at Main$_main_closure1.doCall(Main.groovy:30)
at Main.main(Main.groovy:24)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:143)
以下是主要类的代码:
// Grap HTTPBuilder component from maven repository
@Grab(group='org.codehaus.groovy.modules.http-builder',
module='http-builder', version='0.5.2')
// import of HttpBuilder related stuff
import groovyx.net.http.*
import parsers.Parser
import parsers.WuantoParser
import parsers.Row
class Main {
static mapOfParsers = [:]
static void main(args) {
List<Row> results = new ArrayList<>()
// Initiating the parsers for the ebay-keywords websites
println "Initiating Parsers..."
initiateParsers()
println "Parsing Websites..."
mapOfParsers.each { key, parser ->
switch (key) {
case Constants.Parsers.WUANTO_PARSER:
println "Parsing Url: $Constants.Url.WUANTO_ROOT_CAT_URL"
println "Retrieving Html Content..."
def http = new HTTPBuilder(Constants.Url.WUANTO_ROOT_CAT_URL)
def html = http.get([:])
println "Parsing Html Content..."
results.addAll(((Parser) parser).parseHtml(html))
break
}
}
results.each {
println it
}
}
static void initiateParsers() {
mapOfParsers.put(Constants.Parsers.WUANTO_PARSER , new WuantoParser())
}
static void writeToFile(List<Row> rows) {
File file = "output.txt"
rows.each {
file.write it.toString()
}
}
}
答案 0 :(得分:0)
好吧,让我们看看这里。我尝试在您的代码段中运行代码,但http构建器依赖项版本0.5.2已经过时,并且在我的groovy脚本指向的存储库中无法访问。所以我用更新版本0.7.1替换它。
此外,代码中http.get返回的html变量值实际上是一种解析格式。即它不是文本,而是一个常规的NodeChild对象。这是因为http构建器默认情况下执行html解析,如果需要,你必须明确地告诉它返回纯文本(即使它返回一个阅读器而不是文本)。
以下有些重组和重写的代码版本演示了这个想法:
var text = $.trim($('.amount').html());
var res = text.split(" ");
var num = parseInt(res[0]) + 1;
text = num + " "+ res[1];
$('.amount').html(text);
运行以上打印:
// Grap HTTPBuilder component from maven repository
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.*
import groovy.xml.XmlUtil
import static groovyx.net.http.ContentType.*
class MyParser {
def parseHtml(html) {
[html]
}
}
def mapOfParsers = [:]
mapOfParsers["WUANTO_PARSER"] = new MyParser()
result = []
mapOfParsers.each { key, parser ->
switch (key) {
case "WUANTO_PARSER":
// just a sample url which returns some html data
def url = "https://httpbin.org/links/10/0"
def http = new HTTPBuilder(url)
def html = http.get([:])
// the object returned from http.get is of type
// http://docs.groovy-lang.org/latest/html/api/groovy/util/slurpersupport/NodeChild.html
// this is a parsed format which is navigable in groovy
println "extracting HEAD.TITLE text: " + html.HEAD.TITLE.text()
println "class of returned object ${html.getClass().name}"
println "First 100 characters parsed and formatted:\n ${XmlUtil.serialize(html).take(100)}"
// forcing the returned type to be plain text
def reader = http.get(contentType : TEXT)
// what is returned now is a reader, we can get the text in groovy
// via reader.text
def text = reader.text
println "Now we are getting text, 100 first characters plain text:\n ${text.take(100)}"
result.addAll parser.parseHtml(text)
break
}
}
result.each {
println "result length ${it.length()}"
}
(为简洁起见,省略了一些来自XmlUtil.serialize的警告)。
这些都没有解释为什么你会得到你所获得的例外,但也许上述内容可以让你解锁并解决问题。