jsoup系列教材 (二)- 获取文档

步骤1:获取 Document
步骤2:代码演示

步骤 1 : 获取 Document

获取Document对象的方式有多种,常见的就是基于字符串,文件,网页地址。

步骤 2 : 代码演示

其中 a.html 在下载区(点击进入)的可运行项目里

代码演示

package cn.how2j.jsoup;

import java.io.File;

import java.net.URL;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

public class Test {

    public static void main(String[] args) throws Exception {

        String html1 = "<html><body><p>Hello HTML</p></body></html>";

        Document doc1 = Jsoup.parse(html1);

        System.out.println("基于字符串方式得到的 Document:\r\n"+ doc1);

         

        File f = new File("a.html");

        if(f.exists()) {

            Document doc2 = Jsoup.parse(f,"utf-8");

            System.out.println("基于文件方式得到的 Document:\r\n"+ doc2);

        }

         

        String url = "http://www.baidu.com";

        Document doc3 = Jsoup.parse(new URL(url),5000); //超过5秒就报错

        System.out.println("基于URL方式得到的 Document:\r\n"+ doc3);

         

    }

}


更多内容,点击了解: https://how2j.cn/k/jsoup/jsoup-doc/2150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值