Table of Contents
前言
下载
一个文档的对象模型
获取 Document 对象
解析并提取 HTML 元素
使用传统的操作DOM的方式
选择器
修改获取数据
Home Java javaTutorial Java crawler jsoup parses HTML example analysis

Java crawler jsoup parses HTML example analysis

Apr 25, 2023 pm 01:19 PM
java html jsoup

    前言

    使用python写爬虫的人,应该都听过beautifulsoup4这个包,用来它来解析网页甚是方便。那么在java里有没有类似的包呢?当然有啦!而且也非常好用。下面隆重介绍jsoup

    jsoup 实现了 WHATWG HTML5 规范,能够与现代浏览器解析成相同的DOM。其解析器能够尽最大可能从你提供的HTML文档来创建一个干净的解析结果,无论HTML的格式是否完整。比如它可以处理没有关闭的标签。

    举个栗子:

    <p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p>
    Copy after login

    它也可以处理隐式标签,创建可靠的文档结构(html标签包含head 和 body,在head只出现恰当的元素)。

    下载

    一个文档的对象模型

    • 文档由多个ElementsTextNodes组成 ;

    • 其继承结构如下:Document继承Element继承Node, TextNode继承 Node.

    • 一个Element包含一个子节点集合,并拥有一个父Element。他们还提供了一个唯一的子元素过滤列表。

    获取 Document 对象

    jsoup 可以从包括字符串、URL 地址以及本地文件来加载 HTML 文档,并生成 Document 对象实例。

    // (1)从字符串中获取
    String html = "<html><head><title>First parse</title></head>"
      + "<body><p>Parsed HTML into a doc.</p></body></html>";
    Document doc1 = Jsoup.parse(html);
    // (2)从 URL 直接加载 HTML 文档
    // get方法
    Document doc2 = Jsoup.connect("http://www.163.com").get(); 
    // post方法
    Document doc = Jsoup.connect("http://example.com")
      .data("query", "Java")
      .userAgent("Mozilla")
      .cookie("auth", "token")
      .timeout(3000)
      .post();
      // (3)从文件中加载 HTML 文档
    File input = new File("D:/test.html"); 
    Document doc = Jsoup.parse(input,"UTF-8","http://www.163.com/");
    Copy after login

    常用到的方法如下:

    public static Connection connect(String url)
    public static Document parse(String html, String baseUri)
    public static Document parse(URL url,  int timeoutMillis) throws IOException
    public static Document parse(File in,  String charsetName) throws IOException
    public static Document parse(InputStream in, String charsetName,  String baseUrl)  throws IOException
    Copy after login

    parse方法能够将输入的 HTML 解析为一个新的文档 (Document),只要解析的不是空字符串,就能返回一个结构合理的文档,其中包含(至少) 一个head和一个body元素。
    上面的参数 baseUri的作用是,如果要解析的html中存在相对路径,那么就根据这个参数变成绝对路径, 如果不需要可以传入一个空字符串。

    注:通过connect方法来获得 html 源码,有的时候会遇到乱码的问题,这个时候该怎么办么?方法里有一个 parse 方法,传入参数 InputStreamcharsetName以及baseUrl,所有可以这样解决:

    String url = "http://xxxxxxx";
    Document document = Jsoup.parse(new URL(url).openStream(), "GBK", url);// 以 gbk 编码为栗。
    Copy after login

    Jsoup的强项是解析html,当然了,它能处理一些简单情况,遇到复杂的情形还是使用 httpClient 这个包吧,你值得拥有!

    解析并提取 HTML 元素

    使用传统的操作DOM的方式

    举个栗子

    Element content = doc.getElementById("content");
    Elements links = content.getElementsByTag("a");
    Elements mx = content.getElementsByClass("help");
    Copy after login

    :doc 为 Document 对象。

    还有写常用的方法,比如

    public Elements getElementsByAttributeValue(String key,  String value)
    public Element attr(String attributeKey,  String attributeValue)
    public Elements getAllElements()
    // 获得孩子节点中所有的文本拼接
    public String text()
    // 获得节点的内部html
    public String html()
    Copy after login

    Document 对象还有一个方法

    // 获取标题
    public String title()
    // 获得某节点的html,这个方法继承自Node类,所以Element类也有该方法
    public String outerHtml()
    Copy after login

    选择器

    在元素检索方面,jsoup 的选择器简直无所不能。

    jsoup 选择器很多,这里仅仅举出几个栗子,

    Elements links = doc.select("a[href]"); // 具有href属性的a标签
    Elements pngs = doc.select("img[src$=.png]");// src属性以.png结尾的img标签
    Element masthead = doc.select("div.masthead").first();// class属性为masthead的div标签中的第一个
    Elements resultLinks = doc.select("h4.r &gt; a"); // class属性为r的h4标签的直接子a标签
    Elements resultLinks = doc.select(img[src~=(?i)\.(png|jpe?g)])
    Copy after login

    Selector选择器概述

    tagname: 通过标签查找元素,比如:a

    ns|tag: 通过标签在命名空间查找元素,比如:可以用 fb|name 语法来查找 元素

    #id: 通过ID查找元素,比如:#logo

    .class: 通过class名称查找元素,比如:.masthead

    [attribute]: 利用属性查找元素,比如:[href]

    [^attr]: 利用属性名前缀来查找元素,比如:可以用[^data-] 来查找带有HTML5 Dataset属性的元素

    [attr=value]: 利用属性值来查找元素,比如:[width=500]

    [attr^=value], [attr$=value], [attr*=value]: 利用匹配属性值开头、结尾或包含属性值来查找元素,比如:[href*=/path/]

    [attr~=regex]: 利用属性值匹配正则表达式来查找元素,比如: img[src~=(?i)\.(png|jpe?g)]

    *: 这个符号将匹配所有元素

    Selector选择器组合使用

    el#id: 元素+ID,比如: div#logo

    el.class: 元素+class,比如: div.masthead

    el[attr]: 元素+class,比如: a[href]

    任意组合,比如:a[href].highlight

    ancestor child: 查找某个元素下子元素,比如:可以用.body p 查找在"body"元素下的所有 p元素

    parent > child: 查找某个父元素下的直接子元素,比如:可以用div.content > p 查找 p 元素,也可以用body > * 查找body标签下所有直接子元素

    siblingA + siblingB: 查找在A元素之前第一个同级元素B,比如:div.head + div

    siblingA ~ siblingX: 查找A元素之前的同级X元素,比如:h2 ~ p

    el, el, el:多个选择器组合,查找匹配任一选择器的唯一元素,例如:div.masthead, div.logo

    伪选择器selectors

    :lt(n): 查找哪些元素的同级索引值(它的位置在DOM树中是相对于它的父节点)小于n,比如:td:lt(3) 表示小于三列的元素

    :gt(n):查找哪些元素的同级索引值大于n,比如: div p:gt(2)表示哪些div中有包含2个以上的p元素

    :eq(n): 查找哪些元素的同级索引值与n相等,比如:form input:eq(1)表示包含一个input标签的Form元素

    :has(seletor): 查找匹配选择器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素

    :not(selector): 查找与选择器不匹配的元素,比如: div:not(.logo) 表示不包含 class=logo 元素的所有 div 列表

    :contains(text): 查找包含给定文本的元素,搜索不区分大不写,比如: p:contains(jsoup)

    :containsOwn(text): 查找直接包含给定文本的元素

    :matches(regex): 查找哪些元素的文本匹配指定的正则表达式,比如:div:matches((?i)login)

    :matchesOwn(regex): 查找自身包含文本匹配指定正则表达式的元素

    注: 上述伪选择器索引是从0开始的,也就是说第一个元素索引值为0,第二个元素index为1等。

    对于 Elements 的来历,看这里

    public class Elements extends ArrayList<Element>
    Copy after login

    另外,可以查看Selector API参考来了解更详细的内容,可以看出,jsoup 使用跟 jQuery 一模一样的选择器对元素进行检索,以上的检索方法如果换成是其他的 HTML 解释器,至少都需要很多行代码,而 jsoup 只需要一行代码即可完成。

    修改获取数据

    // 为所有链接增加 rel=nofollow 属性
    doc.select("div.comments a").attr("rel", "nofollow"); 
     // 为所有链接增加 class=mylinkclass 属性
    doc.select("div.comments a").addClass("mylinkclass"); 
    // 删除所有图片的 onclick 属性
    doc.select("img").removeAttr("onclick"); 
    // 清空所有文本输入框中的文本
    doc.select("input[type=text]").val(""); 
    // 获得rel属性的值
    doc.select("div.comments a").attr("rel");
    Copy after login

    The above is the detailed content of Java crawler jsoup parses HTML example analysis. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1659
    14
    PHP Tutorial
    1257
    29
    C# Tutorial
    1231
    24
    How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

    This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

    Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

    Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

    React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

    PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

    PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

    PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

    PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

    See all articles