从一个href标签获取链接Jsoup android

时间:2017-05-01 16:47:07

标签: android html jsoup

我有这个HTML代码...我想从一个href获得它返回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>

所以我想访问

中的链接
<a href="website">

我的代码是

masthea = doc.select(&#34; div [class =&#39; sp-slide&#39;]&#34;);                 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);

                    }
            }

2 个答案:

答案 0 :(得分:0)

试试,

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

href会保存页面的所有链接。在您的情况下,这个html文件中唯一的链接。

答案 1 :(得分:0)

我用这段代码解决了这个问题:

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

这得到第一个href

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

我已编辑此字符串并替换:

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