如何从<a href=""> in nested html elements using jericho?</a>获取文字

时间:2014-02-20 09:55:03

标签: java html parsing jericho-html-parser

我有一些像这样的HTML代码

<div class="itm hasOverlay lastrow">
<a id="3:LE343SPABGLIANID" class="itm-link itm-drk trackingOnClick" title="League Sepatu Casual Geof S/L LO - Hitam/Biru" href="league-sepatu-casual-geof-sl-lo-hitambiru-68166.html" rel="-standard|">
</a>
<div class="itm-overlay itm-group-mainbox-with-group"></div>
</div>

我应该怎么做才能在<{1}}中找到 league-sepatu-casual-geof-sl-lo-hitambiru-68166.html 的文字?< / p>

1 个答案:

答案 0 :(得分:0)

这应该很简单......

Source source=new Source(new StringReader(inputString));
Element aElement = source.getFirstElement(HTMLElementName.A);
String href = aElement.getAttributeValue("href");
System.out.println(href);

...虽然这做了一些假设,当然:即inputString 您发布的字符串(并且此部分未包含在其他标记中) ,而且这部分只包含一个链接(a)。

(如果这些假设无效,则必须以某种方式识别此特定div和正确的a标记。例如,通过搜索具有属性{{div的{​​{1}} 1}}和class="itm hasOverlay lastrow"a - 在任何情况下,都必须更多地了解应从中提取此信息的文档的实际结构