Jsoup解析网页

上一篇文章的HttpClient主要是用来获取网页,Jsoup主要就是用来解析网页。
jsoup提供了非常方便的api让我们能通过DOM,CSS等方法来解析网页,对前端有了解的话就更简单了,就像在js中操作页面元素一样。
jsoup主要功能:
1.从一个URL,文件或字符串中解析HTML;
2.使用DOM或CSS选择器来查找、取出数据
3.可操作HTML元素、属性、文本
在pom.xml文件中添加依赖

  <!--jsoup解析网页-->
    <!--jsoup-->
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.10.2</version>
    </dependency>
    <!--例子在test目录下测试,所以添加测试依赖-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--操作文件所需依赖-->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>
    <!--工具类-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.7</version>
    </dependency>

Jsoup解析URL

    @Test
    public void testurl() throws IOException {
        //解析url地址,第一个参数是访问的url, 第二个参数时访问时候的超时时间
        Document document = Jsoup.parse(new URL("https://www.csdn.net/"), 1000);
        //使用标签选择器,获取title标签中的内容(first表示查询到的标签数组第一个,text表示文本内容)
        String title = document.getElementsByTag("title").first().text();
        System.out.println(title);
    }

这里先给出后面会用到的test.html文件

<html>
 <head> 
  <title>传智播客官网-一样的教育,不一样的品质</title> 
 </head> 
 <body>
	<div class="city">
		<h3 id="city_bj">北京中心</h3>
		<fb:img src="/2018czgw/images/slogan.jpg" class="slogan"/>
		<div class="city_in">
			<div class="city_con" style="display: none;">
				<ul>
					<li id="test" class="class_a class_b">
						<a href="http://www.itcast.cn" target="_blank">
							<span class="s_name">北京</span>
						</a>
					</li>
					<li>
						<a href="http://sh.itcast.cn" target="_blank">
							<span class="s_name">上海</span>
						</a>
					</li>
					<li>
						<a href="http://gz.itcast.cn" target="_blank">
							<span abc="123" class="s_name">广州</span>
						</a>
					</li>
					<ul>
						<li>天津</li>
					</ul>					
				</ul>
			</div>
		</div>
	</div>
 </body>
</html>

Jsoup解析字符串

    @Test
    public void testString() throws IOException {
        //使用工具类读取文件,获取字符串
        String file = FileUtils.readFileToString(new File("D:\\test\\test.html"), "utf-8");
        //解析字符串
        Document document = Jsoup.parse(file);
        String title = document.getElementsByTag("title").first().text();
        System.out.println(title);
    }

Jsoup解析文件

    @Test
    public void testFile() throws IOException {
        //解析文件
        Document document = Jsoup.parse(new File("同上,文件位置"), "utf-8");
        String title = document.getElementsByTag("title").first().text();
        System.out.println(title);
    }

通过dom方式解析文件

    @Test
    public void testDOM() throws IOException {
        //解析文件
        Document document = Jsoup.parse(new File("文件位置"), "utf-8");
        //根据id获取元素
        String test = document.getElementById("city_bj").text();
        //根据标签获取元素
        String a = document.getElementsByTag("a").first().text();
        //根据类名获取元素
        String class_a = document.getElementsByClass("class_a").first().text();
        //根据属性获取元素
        String abc = document.getElementsByAttribute("abc").first().text();
        System.out.println(abc);
    }
    @Test
    public void testData() throws IOException {
        //解析文件
        Document document = Jsoup.parse(new File("C:\\Users\\Simple\\Desktop\\test.html"), "utf-8");
        //根据id获取元素
        Element element = document.getElementById("test");
        //从元素中获取id
        String id = element.id();
        //从元素中获取类名
        String className = element.className();
        //从元素中获取属性值
        String aClass = element.attr("class");
        //从元素中获取所有属性
        Attributes attributes = element.attributes();
        //从元素中获取文本内容
        String text = element.text();
        System.out.println(text);
    }

Selector选择器组合使用

    @Test
    public void testSelector() throws IOException {
        //解析文件
        Document document = Jsoup.parse(new File("C:\\Users\\Simple\\Desktop\\test.html"), "utf-8");
        //通过标签查找元素
        Elements elements = document.select("span");
        for (Element element : elements) {
            System.out.println(element.text());
        }
        //通过ID查找元素
        Element element1 = document.select("#city_bj").first();
        System.out.println(element1.text());
        //通过类名查找元素
        Element classa = document.select(".class_a").first();
        //通过属性查找元素
        Element abc = document.select("[abc]").first();
        System.out.println(abc.text());
        //通过属性值查找元素
        Elements elements1 = document.select("[class=s_name]");
        for (Element element : elements1) {
            System.out.println(element.text());
        }
    }
@Test
    public void testSelector2() throws IOException {
        //解析文件
        Document document = Jsoup.parse(new File("C:\\Users\\Simple\\Desktop\\test.html"), "utf-8");
        //元素+id
        Element element = document.select("h3#city_bj").first();
        //元素+class
        Element element1 = document.select("li.class_a").first();
        //元素+attr
        Element element2 = document.select("span[abc]").first();
        //任意组合
        Element element3 = document.select("span[abc].s_name").first();
        //查找某个元素的子元素
        Element element4 = document.select("ul ul li").first();
        //查找某个元素的直接子元素(第一级)
        Element element5 = document.select(".city_con > ul > li").first();
        //查找某个元素的所有直接子元素
        Elements elements = document.select(".city_con > *");
        for (Element element6 : elements) {
            System.out.println(element6.text());
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值