返回顶部
首页 > 资讯 > 后端开发 > Python >Java爬虫实现Jsoup利用dom方法遍历Document对象
  • 335
分享到

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

2024-04-02 19:04:59 335人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

先给出网页地址: https://wall.alphacoders.com/featured.PHP?lang=Chinese 主要步骤: 利用jsoup的connect方法获取D

先给出网页地址:

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="/file/imgs/upload/202211/13/q3sz2cejppk.jpg" 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;




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对象内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java爬虫实现Jsoup利用dom方法遍历Document对象

本文链接: https://lsjlt.com/news/149016.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Java爬虫实现Jsoup利用dom方法遍历Document对象
    先给出网页地址: https://wall.alphacoders.com/featured.phplang=Chinese 主要步骤: 利用Jsoup的connect方法获取Do...
    99+
    2024-04-02
  • Java爬虫怎么实现Jsoup利用dom方法遍历Document对象
    这篇文章主要讲解了“Java爬虫怎么实现Jsoup利用dom方法遍历Document对象”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java爬虫怎么实现Jsoup利用dom方法遍历Docu...
    99+
    2023-06-30
  • python遍历可迭代对象的实现方法
    目录可迭代(iterable)迭代器(iterator)方法1:使用for循环简单结构遍历方法2:借用 range() 和 len() 函数遍历方法3:借用 iter() 函数遍历方...
    99+
    2023-02-28
    python 遍历可迭代对象 python 可迭代对象
  • js如何使用循环遍历对象方法实现浅拷贝
    这篇文章将为大家详细讲解有关js如何使用循环遍历对象方法实现浅拷贝,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。使用循环遍历对象方法(遍历对象的属性赋值给新对象)function shallow...
    99+
    2023-06-17
  • 怎么在jquery中利用find()方法实现遍历
    这篇文章将为大家详细讲解有关怎么在jquery中利用find()方法实现遍历,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。jquery是什么jquery是一个简洁而快速的JavaScript...
    99+
    2023-06-14
  • 利用OpenCV实现YOLO对象检测方法详解
    目录前言什么是YOLO物体检测器?项目结构检测图像检测视频前言 本文将教你如何使用YOLOV3对象检测器、OpenCV和Python实现对图像和视频流的检测。用到的文件有yolov3...
    99+
    2024-04-02
  • Java使用Arrays.sort()方法实现给对象排序
    目录使用Arrays.sort()方法给对象排序麻烦的方法Arrays.sort()方法浅谈Arrays.sort()原理例子1基础知识点例子2双轴快排另外参考了其他博文,算法思路如...
    99+
    2024-04-02
  • Java编程中,如何利用API实现对象的高效算法?
    Java是一种面向对象的编程语言,它提供了丰富的API,使得开发人员能够更加高效地开发出复杂的应用程序。在Java编程中,对象是非常重要的概念,而高效的算法则是保证程序性能的关键。在本文中,我们将介绍如何利用Java API实现对象的高效...
    99+
    2023-10-24
    对象 编程算法 api
  • Java如何使用Arrays.sort()方法实现给对象排序
    这篇文章主要介绍了Java如何使用Arrays.sort()方法实现给对象排序,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。使用Arrays.sort()方法给对象排序当我们...
    99+
    2023-06-22
  • 如何在Java中利用对象和数组实现高效的编程算法?
    Java是一种广泛使用的编程语言,因为它具有可移植性、可扩展性和可重用性等优点。在Java中,利用对象和数组实现高效的编程算法是非常常见的。本文将介绍如何在Java中利用对象和数组实现高效的编程算法,为大家提供一些实用的技巧和经验。 一、利...
    99+
    2023-10-31
    对象 编程算法 数组
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作