使用jsoup从Div标签的内部标签获取属性值

时间:2014-11-25 09:29:14

标签: jsoup

我想用jsoup从网页中提取内容。值在内部标记中如何提取这些值?

例如

< div id="tfm_skyscraper" class="top_right_skyscraper"></div>
  <nav class="main group">
   <section class="verticals world group" data-beacon="{&quot;p&quot">
    <ul class="verticals-ul">
        <li class="front-page toplevel" data-beacon="{&quot">
        <a class="toplevel-a" href="http://www.huffingtonpost.com" title="Home"  tabindex="1" sl-processed="1">FRONT PAGE</a>*

        </li>                           
    </ul>
 </section>
</nav>

我想在锚标记* 中提取内容 FRONT PAGE 如何做到这一点?

1 个答案:

答案 0 :(得分:0)

这将使用类toplevel-a

打印所有链接标记的文本
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {
        String html =   "<div id=\"tfm_skyscraper\" class=\"top_right_skyscraper\"></div>" +
                            "<nav class=\"main group\">" +
                                "<section class=\"verticals world group\" data-beacon=\"{&quot;p&quot\">" +
                                    "<ul class=\"verticals-ul\">" +
                                        "<li class=\"front-page toplevel\" data-beacon=\"{&quot\">" +
                                            "<a class=\"toplevel-a\" href=\"http://www.huffingtonpost.com\" title=\"Home\" tabindex=\"1\" sl-processed=\"1\">FRONT PAGE</a>*" +
                                        "</li>" +                           
                                    "</ul>" +
                                "</section>" +
                            "</nav>";

        Document doc = Jsoup.parse(html);
        Elements els = doc.select("a.toplevel-a");
        for(Element el : els) {
            System.out.println(el.text());
        }
    }
}