【爬虫】Java 爬虫组件 Jsoup

【爬虫】Java 爬虫组件 Jsoup

写在前面

爬虫是通过编程的方式,从网站上获取数据的一种方式。很多语言都提供的有爬虫功能,都大同小异。这篇文章分享的是 Java 的爬虫组件 jsoup 的使用。以下案例以爬去小说为例

实现思路和步骤

步骤一:引入 Jsoup

  • 普通的 Java 项目需要手动下载 jsoup jar 包,导入到项目中。

  • Maven 项目可以导入 jsoup 坐标

    xml 复制代码
     <dependency>
       <groupId>org.jsoup</groupId>
       <artifactId>jsoup</artifactId>
       <version>1.13.1</version>
     </dependency>

步骤二:获取页面组件内容

java 复制代码
// 借助 jsoup 工具获取网站的信息
// 获取链接对象
Connection connect = Jsoup.connect(site);
// 获取网站的文档对象
Document document = connect.get();

步骤三:分析页面构成获取需要的组件

  • 页面是通过 HTML 标签组成的,可以使用浏览器的开发者工具,找到需要获取的组件。
  • 分析组件的特征,看是否有 id、class 等相关的标识。
  • 根据标识获取组件对象,注意除 id 外,其他标识获取到的都是多个。
  • 有些数据在 HTML 的标签属性中,比如超链接、图片的请求地址在 href、src 属性中,所以还需要搭配attr()方法获取。

    下图则是通过浏览器开发者工具分析的截图:
    1、章节的正文在<ul class="chapterlist"></ul>中。
    2、章节的名称在 ul li 的超链接中。
    3、章节的连接在超链接的 href 属性中。

代码案例

java 复制代码
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

/**
 * 爬虫案例:爬取数据写入到磁盘。
 * 1、文本
 * 2、图片
 * 爬取小说:
 * 1、小说的网址。
 * 2、分析网站的构成,获取需要的信息。
 * 2.1 HTML的技术
 */
public class JsoupDemo {
    public static void main(String[] args) throws IOException {
        // 网站地址
        String site = "https://www.69shuba.cc/book/64272/";

        // 借助 jsoup 工具获取网站的信息
        // 获取链接对象
        Connection connect = Jsoup.connect(site);
        // 获取网站的文档对象
        Document document = connect.get();

        // 从文档对象中获取需要的组件
        Elements chapterList = document.getElementsByClass("chapterlist");
        Element ul = chapterList.get(1);

        // 获取所有的 a 标签
        Elements as = ul.getElementsByTag("a");

        File file = new File("D://a.txt");
        if(!file.exists()){
            file.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(file, true);

        // 遍历所有的 a 元素
        for (Element a : as) {
            // 得到章节名称
            String chapterName = a.text() +  "\r\n";

            // 把章节写入到文件
            fos.write(chapterName.getBytes());

            // 获取的是超链接的 href 属性
            String href = a.attr("href");
            href = "https://www.69shuba.cc/book/64272/" + href;

            // 请求章节地址
            Connection chapterConnection = Jsoup.connect(href);
            Document chapterDocument = chapterConnection.get();

            // 获取需要的组件
            Element htmlContent = chapterDocument.getElementById("htmlContent");
            String content = htmlContent.text();

            // 把章节写入到文件
            fos.write(content.getBytes());
            fos.write("\r\n\r\n".getBytes());

            System.out.println("《" + a.text() + "》" + "下载完成!");

            try {
                Random random = new Random();
                Thread.sleep(random.nextInt(5000));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        fos.close();
        System.out.println("小说下载完成!");
    }
}
相关推荐
论迹2 分钟前
【JavaEE】-- 多线程(初阶)2
java·开发语言·java-ee
桃子是唯一的水果11 分钟前
java 单例模式(Lazy Initialization)实现遍历文件夹下所有excel文件且返回其运行时间
java·单例模式·maven
+72013 分钟前
如何在java中用httpclient实现rpc post 请求
java·开发语言·rpc
ybq1951334543114 分钟前
javaEE-SpringBoot日志
java·spring boot·后端
火烧屁屁啦18 分钟前
【JavaEE进阶】图书管理系统 - 贰
java·spring
xzzd_jokelin18 分钟前
Spring AI 接入 DeepSeek:开启智能应用的新篇章
java·人工智能·spring·ai·大模型·rag·deepseek
学习两年半的Javaer21 分钟前
Rust语言基础知识详解【一】
开发语言·rust
PyAIGCMaster22 分钟前
50周学习go语言:第四周 函数与错误处理深度解析
开发语言·学习·golang
全栈开发圈23 分钟前
新书速览|Rust汽车电子开发实践
开发语言·rust·汽车
PyAIGCMaster24 分钟前
第二周补充:Go语言中&取地址符与fmt函数详解
开发语言·后端·golang