引入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
代码示例
package *;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @program: simple_tools
* @description: Jsoup网页爬虫工具
* @author: ChenWenLong
* @create: 2019-10-22 14:00
**/
public class JsoupUtil {

//=========================标签名称=========================
private static final String TAG_NAME_TITLE = "title";
private static final String TAG_NAME_IMAGE = "img";

/**
* 功能描述:
* 〈GET方法发请求〉
*
* @params : [url]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:17
*/
public static Document connectByGet(String url) throws IOException {
return getDocument(url,0).get();
}

/**
* 功能描述:
* 〈获取响应结果〉
*
* @params : [url]
* @return : java.lang.String
* @author : cwl
* @date : 2019/10/22 14:40
*/
public static String getRespone(String url) throws IOException {
Connection connection = getDocument(url, 0);
return connection.response().body();
}

/**
* 功能描述:
* 〈设置cookies获取响应〉
*
* @params : [url, cookies]
* @return : java.lang.String
* @author : cwl
* @date : 2019/10/22 14:42
*/
public static String getRespone(String url,Map cookies) throws IOException {
Connection connection = getDocument(url, 0);
connection.cookies(cookies);
return connection.response().body();
}


/**
* 功能描述:
* 〈GET方法发请求〉
*
* @params : [url, timeout]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:23
*/
public static Document connectByGet(String url,int timeout) throws IOException {
return getDocument(url,timeout).get();
}

/**
* 功能描述:
* 〈POST方法发请求〉
*
* @params : [url]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:17
*/
public static Document connectByPost(String url) throws IOException {
return getDocument(url,0).post();
}

/**
* 功能描述:
* 〈POST方法发请求〉
*
* @params : [url]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:23
*/
public static Document connectByPost(String url,int timeout) throws IOException {
return getDocument(url,timeout).post();
}

/**
* 功能描述:
* 〈获得网页文档信息〉
*
* @params : [url]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:04
*/
public static Document getDocument(String url) throws IOException {
return getDocument(url,0).post();
}

/**
* 功能描述:
* 〈获得网页文档信息 - 设置cookies信息〉
*
* @params : [url, cookies]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:43
*/
public static Document getDocument(String url,Map cookies) throws IOException {
return getDocument(url,0).cookies(cookies).post();
}

/**
* 功能描述:
* 〈获得网页文档信息-设置连接超时时间〉
*
* @params : [url, timeout]
* @return : org.jsoup.nodes.Document
* @author : cwl
* @date : 2019/10/22 14:05
*/
public static Connection getDocument(String url,int timeout) throws IOException {
Connection connect = Jsoup.connect(url);
connect.timeout(timeout);
return connect;
}

/**
* 功能描述:
* 〈获得网页文档信息-设置连接超时时间/设置cookies〉
*
* @params : [url, timeout, cookies]
* @return : org.jsoup.Connection
* @author : cwl
* @date : 2019/10/22 14:44
*/
public static Connection getDocument(String url,int timeout,Map cookies) throws IOException {
Connection connect = Jsoup.connect(url);
connect.timeout(timeout).cookies(cookies);
return connect;
}

/**
* 功能描述:
* 〈获得网页文档信息-设置连接超时时间/设置cookies/设置请求头〉
*
* @params : [url, timeout, cookies, headers]
* @return : org.jsoup.Connection
* @author : cwl
* @date : 2019/10/22 14:45
*/
public static Connection getDocument(String url,int timeout,Map cookies,Map headers) throws IOException {
Connection connect = Jsoup.connect(url);
connect.timeout(timeout).cookies(cookies).headers(headers);
return connect;
}

/**
* 功能描述:
* 〈获取网页所有文本信息 - 配置连接超时时间〉
*
* @params : [url, timeout]
* @return : java.util.List<java.lang.String>
* @author : cwl
* @date : 2019/10/22 14:09
*/
public static List<String> getEachText(String url,int timeout) throws IOException {
Connection connect = Jsoup.connect(url);
Document document = connect.timeout(timeout).get();
Elements allElements = document.getAllElements();
return allElements.eachText();
}

/**
* 功能描述:
* 〈获取网页所有文本信息〉
*
* @params : [url]
* @return : java.util.List<java.lang.String>
* @author : cwl
* @date : 2019/10/22 14:11
*/
public static List<String> getEachText(String url) throws IOException {
return getEachText(url,0);
}

/**
* 功能描述:
* 〈获取网页Body元素〉
*
* @params : [url, timeout]
* @return : org.jsoup.nodes.Element
* @author : cwl
* @date : 2019/10/22 14:07
*/
public static Element getBody(String url,int timeout) throws IOException {
return connectByPost(url,timeout).body();
}

/**
* 功能描述:
* 〈获取网页Body元素〉
*
* @params : [url]
* @return : org.jsoup.nodes.Element
* @author : cwl
* @date : 2019/10/22 14:12
*/
public static Element getBody(String url) throws IOException {
return getBody(url,0);
}

/**
* 功能描述:
* 〈获取网页Head头部〉
*
* @params : [url]
* @return : org.jsoup.nodes.Element
* @author : cwl
* @date : 2019/10/22 14:50
*/
public static Element getHead(String url) throws IOException {
return connectByPost(url,0).head();
}

/**
* 功能描述:
* 〈获取网页Head头部〉
*
* @params : [url, timeout]
* @return : org.jsoup.nodes.Element
* @author : cwl
* @date : 2019/10/22 14:51
*/
public static Element getHead(String url,int timeout) throws IOException {
return connectByPost(url,timeout).head();
}

/**
* 功能描述:
* 〈获得地址标题〉
*
* @params : [url, timeout]
* @return : java.lang.String
* @author : cwl
* @date : 2019/10/22 14:55
*/
public static String getTitle(String url,int timeout) throws IOException {
return getHead(url,timeout).getElementsByTag(TAG_NAME_TITLE).text();
}

/**
* 功能描述:
* 〈获得地址标题〉
*
* @params : [url]
* @return : java.lang.String
* @author : cwl
* @date : 2019/10/22 14:56
*/
public static String getTitle(String url) throws IOException {
return getHead(url,0).getElementsByTag(TAG_NAME_TITLE).text();
}

/**
* 功能描述:
* 〈获取所有的图片〉
*
* @params : [url]
* @return : java.util.List<java.lang.String>
* @author : cwl
* @date : 2019/10/22 15:00
*/
public static List<String> getAllImage(String url) throws IOException {
return getBody(url).getElementsByTag(TAG_NAME_IMAGE).eachText();
}

/**
* 功能描述:
* 〈获取所有的图片〉
*
* @params : [url, timeout]
* @return : java.util.List<java.lang.String>
* @author : cwl
* @date : 2019/10/22 15:00
*/
public static List<String> getAllImage(String url,int timeout) throws IOException {
return getBody(url).getElementsByTag(TAG_NAME_IMAGE).eachText();
}

}