不正な HTML ページを解析し、そこから特定の URL を任意の種類のコレクションとして抽出する必要があります。コレクションの種類はあまり気にしません。それを繰り返し処理できるようにする必要があるだけです。
次のような構造があるとします。
<html>
<body>
<div class="outer">
<div class="inner">
<a href="http://www.google.com" title="Google">Google-Link</a>
<a href="http://www.useless.com" title="I don't need this">Blah blah</a>
</div>
<div class="inner">
<a href="http://www.youtube.com" title="Youtube">Youtube-Link</a>
<a href="http://www.useless2.com" title="I don't need this2">Blah blah2</a>
</div>
</div>
</body>
</html>
そして、これが私がこれまでに行っていることです:
// tagsoup version 1.2 is under apache license 2.0
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2' )
XmlSlurper slurper = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser());
GPathResult nodes = slurper.parse("test.html");
def links = nodes."**".findAll { it.@class == "inner" }
println links
私は何かが欲しい
["http://google.com", "http://youtube.com"]
しかし、私が得るのは次のとおりです。
["Google-LinkBlah blah", "Youtube-LinkBlah blah2"]
より正確に言うと、すべての URL を使用することはできません。なぜなら、解析する必要がある HTML ドキュメントは約 15,000 行の長さで、必要のない URL がたくさんあるからです。したがって、各「内部」ブロックの最初のURL が必要です。