Java爬虫实现Jsoup利用dom方法遍历Document对象

 更新时间:2022年05月17日 16:49:51   作者:Cyril_KI  
本文主要介绍了Java爬虫实现Jsoup利用dom方法遍历Document对象,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

先给出网页地址:

https://wall.alphacoders.com/featured.php?lang=Chinese

主要步骤:

利用Jsoup的connect方法获取Document对象

String html = "https://wall.alphacoders.com/featured.php?lang=Chinese";
Document doc = Jsoup.connect(html).get();

内容过长,就不再显示。

我们以这部分为例:

<ul class="nav nav-pills"> 
    <li><a href="https://alphacoders.com/site/about-us" rel="external nofollow"  rel="external nofollow"  >About Us</a></li> 
    <li><a href="https://alphacoders.com/site/faq" rel="external nofollow"  rel="external nofollow"  >FAQ</a></li> 
    <li><a href="https://alphacoders.com/site/privacy" rel="external nofollow"  rel="external nofollow"  >Privacy Policy</a></li> 
    <li><a href="https://alphacoders.com/site/tos" rel="external nofollow"  rel="external nofollow"  >Terms Of Service</a></li> 
    <li><a href="https://alphacoders.com/site/acceptable_use" rel="external nofollow"  rel="external nofollow"  >Acceptable Use</a></li> 
    <li><a href="https://alphacoders.com/site/etiquette" rel="external nofollow"  rel="external nofollow"  >Etiquette</a></li> 
    <li><a href="https://alphacoders.com/site/advertising" rel="external nofollow"  rel="external nofollow"  >Advertise With Us</a></li> 
    <li><a id="change_consent">Change Consent</a></li> 
</ul> 

我们先找到所有的ul:

Elements elements = doc.getElementsByTag("ul");

输出如下:

<ul class="nav navbar-nav center"> 
 <li> <a title="Submit Wallpapers" href="https://alphacoders.com/site/submit-wallpaper" rel="external nofollow" ><i class="el el-circle-arrow-up"></i> 提交</a> </li> 
 <li> <a href="https://alphacoders.com/contest" rel="external nofollow" ><i class="el el-gift"></i> 精美奖品</a> </li> 
</ul>
<ul class="nav navbar-nav navbar-right center"> 
 <li> <a href="language.php?lang=Chinese" rel="external nofollow" > <img src="https://static.alphacoders.com/wa/Chinese-flag.png" alt="Chinese-flag">   中文    </a> </li> 
 <li> <a  href="https://alphacoders.com/users/login" rel="external nofollow" ><i class="el el-user"></i> 登录</a> </li> 
 <li> <a href="https://alphacoders.com/users/register" rel="external nofollow" ><i class="el el-edit"></i> 注册</a> </li> 
</ul>
<ul class="pagination"> 
 <li class="active"><a id="prev_page" href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >&lt; 上一页</a></li> 
 <li class="active"><a>1</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >2</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=3" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >3</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=4" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >4</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=5" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >5</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=6" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >6</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=7" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >7</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=8" rel="external nofollow" >8</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=9" rel="external nofollow" >9</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=10" rel="external nofollow" >10</a></li> 
 <li><a>...</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=319" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >319</a></li> 
 <li><a id="next_page" href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >下一页 &gt;</a></li> 
</ul>
<ul class="pagination"> 
 <li class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >&lt; 上一页</a></li> 
 <li class="active"><a>1</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >2</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=3" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >3</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=4" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >4</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=5" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >5</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=6" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >6</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=7" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >7</a></li> 
 <li><a>...</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=319" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >319</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >下一页 &gt;</a></li> 
</ul>
<ul class="pagination"> 
 <li class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >&lt;&lt; </a></li> 
 <li class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >&lt; 上一页</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >下一页 &gt;</a></li> 
 <li><a title="末页 (319)" href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=319" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" > &gt;&gt;</a></li> 
</ul>
<ul class="pagination"> 
 <li class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >1</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=2" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >2</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=3" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >3</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=4" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >4</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=5" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >5</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=6" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >6</a></li> 
 <li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&amp;page=7" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >7</a></li> 
</ul>
<ul class="nav nav-pills"> 
 <li><a href="https://alphacoders.com/site/about-us" rel="external nofollow"  rel="external nofollow"  >About Us</a></li> 
 <li><a href="https://alphacoders.com/site/faq" rel="external nofollow"  rel="external nofollow"  >FAQ</a></li> 
 <li><a href="https://alphacoders.com/site/privacy" rel="external nofollow"  rel="external nofollow"  >Privacy Policy</a></li> 
 <li><a href="https://alphacoders.com/site/tos" rel="external nofollow"  rel="external nofollow"  >Terms Of Service</a></li> 
 <li><a href="https://alphacoders.com/site/acceptable_use" rel="external nofollow"  rel="external nofollow"  >Acceptable Use</a></li> 
 <li><a href="https://alphacoders.com/site/etiquette" rel="external nofollow"  rel="external nofollow"  >Etiquette</a></li> 
 <li><a href="https://alphacoders.com/site/advertising" rel="external nofollow"  rel="external nofollow"  >Advertise With Us</a></li> 
 <li><a id="change_consent">Change Consent</a></li> 
</ul>

可以发现class为"nav nav-pills"的只有一个,我们找到它:

Elements elements = doc.getElementsByTag("ul");
        //System.out.println(elements);
Element tempElement = null;
for(Element element : elements) {
    if (element.className().equals("nav nav-pills")) {
        tempElement = element;
        //System.out.println(element.className());
        break;
    }
}

循环遍历这个ul,输出其中每一个li里每一个a的href和rel属性:

Elements li = tempElement.getElementsByTag("li");
for(Element element : li) {
    Elements element2 = element.getElementsByTag("a");
    for(Element element3 : element2) {
        String hrefString = element3.attr("href");
        String relString = element3.attr("rel");
        if(hrefString != "" && relString != "") {
            System.out.println("href=" + hrefString + " rel="external nofollow"  rel="external nofollow"  " + "rel=" + relString);
        }
    }
}

最终结果:

href=https://alphacoders.com/site/about-us rel=nofollow
href=https://alphacoders.com/site/faq rel=nofollow
href=https://alphacoders.com/site/privacy rel=nofollow
href=https://alphacoders.com/site/tos rel=nofollow
href=https://alphacoders.com/site/acceptable_use rel=nofollow
href=https://alphacoders.com/site/etiquette rel=nofollow
href=https://alphacoders.com/site/advertising rel=nofollow

完整代码:

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import org.jsoup.Jsoup;


/** 
 * @ClassName: Jsoup_Test
 * @description: 
 * @author: KI
 * @Date: 2020年8月17日 下午8:15:14
 */

public class Jsoup_Test {

    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        
        String html = "https://wall.alphacoders.com/featured.php?lang=Chinese";
        Document doc = Jsoup.connect(html).get();
        
        System.out.println(doc);
        Elements elements = doc.getElementsByTag("ul");
        //System.out.println(elements);
        Element tempElement = null;
        for(Element element : elements) {
            if (element.className().equals("nav nav-pills")) {
                tempElement = element;
                //System.out.println(element.className());
                break;
            }
        }
        System.out.println(tempElement);
        Elements li = tempElement.getElementsByTag("li");
        for(Element element : li) {
            Elements element2 = element.getElementsByTag("a");
            for(Element element3 : element2) {
                String hrefString = element3.attr("href");
                String relString = element3.attr("rel");
                if(hrefString != "" && relString != "") {
                    System.out.println("href=" + hrefString + " rel="external nofollow"  rel="external nofollow"  " + "rel=" + relString);
                }
            }
        }        

    }

}

到此这篇关于Java爬虫实现Jsoup利用dom方法遍历Document对象的文章就介绍到这了,更多相关Java Jsoup遍历Document对象内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 一篇文章从无到有详解Spring中的AOP

    一篇文章从无到有详解Spring中的AOP

    。Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的,这篇文章主要给大家介绍了关于Spring中AOP的相关资料,需要的朋友可以参考下
    2021-08-08
  • maven依赖包冲突SLF4J: Class path contains multiple SLF4J bindings处理方法

    maven依赖包冲突SLF4J: Class path contains multiple SLF4J bi

    这篇文章主要给大家介绍了关于maven依赖包冲突SLF4J: Class path contains multiple SLF4J bindings的处理方法,这个问题通常是因为项目中存在多个SLF4J的实现绑定(bindings)导致的冲突,需要的朋友可以参考下
    2024-02-02
  • Spring Boot实现对文件进行压缩下载功能

    Spring Boot实现对文件进行压缩下载功能

    在Web应用中,文件下载功能是一个常见的需求,特别是当你需要提供用户下载各种类型的文件时,本文将演示如何使用Spring Boot框架来实现一个简单而强大的文件下载功能,需要的朋友跟随小编一起学习吧
    2023-09-09
  • Reactor定制一个生产的WebClient实现示例

    Reactor定制一个生产的WebClient实现示例

    这篇文章主要为大家介绍了Reactor定制一个生产的WebClient实现示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • spring security 5.x实现兼容多种密码的加密方式

    spring security 5.x实现兼容多种密码的加密方式

    spring security针对该功能有两种实现方式,一种是简单的使用加密来保证基于 cookie 的 token 的安全,另一种是通过数据库或其它持久化存储机制来保存生成的 token。这篇文章主要给大家介绍了关于spring security 5.x实现兼容多种密码的加密方式,需要的朋友可以参考下。
    2018-01-01
  • Spring Security整合KeyCloak保护Rest API实现详解

    Spring Security整合KeyCloak保护Rest API实现详解

    这篇文章主要为大家介绍了Spring Security整合KeyCloak保护Rest API实现实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • SpringBoot中获取微信用户信息的方法

    SpringBoot中获取微信用户信息的方法

    这篇文章主要介绍了SpringBoot中获取微信用户信息的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • java操作Redis缓存设置过期时间的方法

    java操作Redis缓存设置过期时间的方法

    这篇文章主要介绍了java操作Redis缓存设置过期时间的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Java线程池的优点及池化技术的应用

    Java线程池的优点及池化技术的应用

    这篇文章主要介绍了Java线程池的优点及池化技术的应用,Java种提高程序的执行效率有两种实现方法,一个是使用线程、另一个是使用线程池,下文我们就来具体介绍该详细内容吧,需要的小伙伴可以参考一下
    2022-05-05
  • Java实现Kruskal算法的示例代码

    Java实现Kruskal算法的示例代码

    Kruskal算法是一种用来寻找最小生成树的算法,由Joseph Kruskal在1956年发表。用来解决同样问题的还有Prim算法和Boruvka算法等。本文将介绍用Java语言实现Kruskal算法的示例代码,需要的可以参考一下
    2022-07-07

最新评论