zoukankan      html  css  js  c++  java
  • Jsoup之解析URL、字符串、文件

    package jsoup;

    import org.apache.commons.io.FileUtils;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.junit.Test;

    import java.io.File;
    import java.net.URL;

    public class JsoupFirstTest {
    @Test
    public void testUrl() throws Exception{
    //解析URL地址
    Document doc =Jsoup.parse(new URL("http://www.itcast.cn"),1000);
    //使用标签选择器,获取title标签中的内容
    String title=doc.getElementsByTag("title").first().text();
    //将获取到的信息进行打印
    System.out.println(title);
    }
    @Test
    public void testString() throws Exception{
    //使用工具类读取文件,获得字符串
    String content= FileUtils.readFileToString(new File("C:\Users\Administrator\Desktop\bolg_add.html"),"utf8");
    //解析字符串
    Document doc=Jsoup.parse(content);
    String string=doc.getElementsByTag("title").first().text();
    System.out.println(string);
    }
    @Test
    public void testFile() throws Exception{
    Document doc=Jsoup.parse(new File("C:\Users\Administrator\Desktop\bolg_add.html"),"utf8");
    String title=doc.getElementsByTag("title").first().text();
    System.out.println(title);
    }
    }
    上面代码就是利用jsoup对URL、字符串、文件的解析,这也是Jsoup的三大常用功能。
    在进行以上代码的验证是,必须提前进行依赖的引入。
  • 相关阅读:
    数据结构 树(下)
    数据结构 树(上)
    Python Scrapy爬虫(上)
    线程的生命周期
    多线程-方式二实现Runnable接口方式
    Thread类中的常用的方法及概述
    多线程-方式一继承Thread方式
    14: 字母的前趋或后继
    13: A+B
    12: 成绩转换
  • 原文地址:https://www.cnblogs.com/juddy/p/13121829.html
Copyright © 2011-2022 走看看