javahtmlparsingjericho-html-parser

how to get text from <a href> in nested html elements using jericho?


I have some html code like this

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

What should I do to get text league-sepatu-casual-geof-sl-lo-hitambiru-68166.html in

<a href="league-sepatu-casual-geof-sl-lo-hitambiru-68166.html">?


Solution

  • That should be rather simple...

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

    ... although this makes some assumptions, of course: Namely, that the inputString is only the string that you posted (and that this part is not enclosed in other tags), and that this part only contains a single link (a).

    (If these assumptions are not valid, one somehow has to identify this particular div and the correct a tag. For example, by searching for a div with the attribute class="itm hasOverlay lastrow" and for a a with the class class="itm-link itm-drk trackingOnClick" - in any case, one has to know more about the actual structure of the document from which this information should be extracted)