Mohamed Hocine
Mohamed Hocine

Reputation: 595

Getting link from a href tag Jsoup android

i have this html code ... and i want get the link from a href it return null

<div class="sp-slide">
<div class="sp-layer social" data-position="topRight" data-show-transition="down" data-show-delay="400" data-horizontal="10px">
<a href="https://www.facebook.com/sharer/sharer.php?u=website">
<img src="website" alt="facebook" />
</a>
<a href="website">
<img src="website" />
</a>
</div>
<ul class="sp-layer video-album" data-position="topRight" data-show-transition="down" data-show-delay="400" data-horizontal="76px">
</ul>
<a href="articles/web.html">
<img class="sp-image" src="website" />
<header class="sp-layer" data-position="topLeft" data-show-transition="right" data-show-delay="400">
<div class="meta-m">
<span>5</span>
<img src="website" alt="comments" />
<span>12341</span>
<img src="website" alt="views" />
<span>2017/04/30</span>
<img src="website" alt="date" />
</div>
</header>
<footer class="sp-layer" data-position="bottomLeft" data-show-transition="left" data-show-delay="400">
<h4>some Text</h4>
<h3>
some text
</h3>
</footer>
</a>
</div>

so i want to access to link in

<a href="website">

my code is

masthea = doc.select("div[class='sp-slide']"); for (Element beers : masthea) {

                    imm = masthea.get(beers.siblingIndex()).getElementsByTag("a").get(2).attr("href");


                    Elements elem = beers.select("footer[class='sp-layer']");
                    for (Element ele : elem) {
                        RssItem rss = new RssItem(ele.getElementsByTag("h4").text(), ele.getElementsByTag("h3").text(),beers.attr("abs.href"), beers.getElementsByClass("sp-image").attr("src"));
                        rssItemList.add(rss);

                    }
            }

Upvotes: 1

Views: 871

Answers (2)

Mohamed Hocine
Mohamed Hocine

Reputation: 595

i have solved the problem with this code :

beers.getElementsByTag("a").attr("abs:href")

this get the first a href

<a href="https://www.facebook.com/sharer/sharer.php?u=website">

and i have edit this string and replace :

"https://www.facebook.com/sharer/sharer.php?u=

Upvotes: 0

Flika205
Flika205

Reputation: 552

Try that,

Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a"); 
     for (Element link : links) {
         String href = link.attr("href");
}

href would hold all the links of the page. In your case, the only link in this html file.

Upvotes: 0

Related Questions