我正在尝试抓取一些看起来像这样的 DOM:
<span>text</span>
有时看起来像这样:
<span><p>text</p></span>
但是,我似乎无法弄清楚如何进入text
第二种情况。我尝试了几种方法,这就是我认为应该在下面起作用的方法:
def html = slurper.parse(reader)
Collection<NodeChild> nodes = html.'**'.findAll { it.name() == 'span' && it.@class == 'style2' }
...
def descriptionNode = html.'**'.find { it.name() == 'span' && it.@class == 'style20' }
def innerNode = descriptionNode.'**'.find { it.name() == 'p' }
def description
if (innerNode?.size() > 0)
{
description = innerNode.text()
}
else
{
description = descriptionNode.text()
}
知道我需要如何使用 xmlslurper 来获得我需要的行为吗?