前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java工具集-Jsoup网页爬虫工具

Java工具集-Jsoup网页爬虫工具

作者头像
cwl_java
发布2019-10-26 21:39:17
8050
发布2019-10-26 21:39:17
举报
文章被收录于专栏:cwl_Javacwl_Java
引入依赖
代码语言:javascript
复制
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.1</version>
        </dependency>
代码示例
代码语言:javascript
复制
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();
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引入依赖
  • 代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档