java jsoup解析如何解析html

时间:2016-08-30 22:14:01

标签: java jsoup

有没有办法解析

<a href="/summoner/userName=Huhi" class="Link">Huhi</a> 

in html:

<a href="/summoner/userName=Huhi" class="Link">Huhi</a>
<a href="/summoner/userName=Huhi" class="Link">White</a>
<a href="/summoner/userName=Huhi" class="Link">Angle</a>

输出:

Huhi
White
Angle

2 个答案:

答案 0 :(得分:1)

创建您的文档并获取所有[href]链接,遍历这些链接并获取它们包含的文本。像这样:

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

答案 1 :(得分:1)

您只需选择a并重复elements并打印

即可
String html ="<a href=\"/summoner/userName=Huhi\" class=\"Link\">Huhi</a>\n" +
                "<a href=\"/summoner/userName=Huhi\" class=\"Link\">White</a>\n" +
                "<a href=\"/summoner/userName=Huhi\" class=\"Link\">Angle</a>";

        Document doc = Jsoup.parse(html);
        Elements links = doc.select("a");
        for (Element link : links) {
            System.out.println(link.text());

        }

如需进一步参考,请查看此链接selector-syntax