Java爬虫入门五

使用Jsoup进行html元素的解析

<!--Jsoup依赖 -->
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

 导入commons包方便进行文件以及字符串的解析

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

 方便查找依赖maven仓库

 代码

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.net.URL;

/**
 * jsoup解析html
 */
public class JsoupTest1 {
    public static void main(String[] args) throws Exception {
        // 解析url
        Document doc = Jsoup.parse(new URL("http://www.itcast.cn"), 1000);
        // 解析html
        Jsoup.parse("test.html");
        // 解析文件
        Jsoup.parse(new File("test.html"), "utf8");

        // 使用标签选择器,获取内容
        Element title = doc.getElementsByTag("title").first();
        System.out.println(title.text());
        // 通过id选择器获取内容
        Element city_bi = doc.getElementById("city_bi");
        System.out.println(city_bi.text());
        // 通过class获取
        Element class_a = doc.getElementsByClass("class_a").first();
        System.out.println(class_a.text());
        // 通过属性获取
        Elements adc = doc.getElementsByAttribute("adc");
        Elements elementsByAttributeValue = doc.getElementsByAttributeValue("adc", "sh");
        System.out.println(adc.text());
    }
}

 

posted @   初见洞洞拐  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示