使用jSoup解析内部html标签 [英] Parse the inner html tags using jSoup

查看:108
本文介绍了使用jSoup解析内部html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用Jsoup库的网站中找到重要的链接。因此,假设我们有以下代码:

I want to find the important links in a site using Jsoup library. So for this suppose we have following code:

<h1><a href="http://example.com">This is important </a></h1>

现在解析我们如何发现标签a位于h1标签内?

Now while parsing how can we find that the tag a is inside the h1 tag?

推荐答案

您可以这样做:

You can do it this way:

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Elements headlinesCat1 = doc.getElementsByTag("h1");
for (Element headline : headlinesCat1) {
    Elements importantLinks = headline.getElementsByTag("a");
    for (Element link : importantLinks) {
        String linkHref = link.attr("href");
        String linkText = link.text();
        System.out.println(linkHref);
    }
}

取自 JSoup Cookbook

这篇关于使用jSoup解析内部html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆