java利用jsoup包实现获取网页源代码功能

package com.obcy.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class GetHTML {

    public static String getHTML(String targetUrl){

        //1.确定url
        //String url = "https://picjumbo.com/free-stock-photos/love/";
        String url = targetUrl;
        //2.设置请求方式
        HttpGet hg = new HttpGet(url);
        hg.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
        //3.创建发送对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //4.发送请求,获取响应对象
        //CloseableHttpResponse里面封装了响应头响应体响应行
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(hg);
        } catch (IOException e) {
            e.printStackTrace();
        }


        String html="";
        //5.解析响应对象
        if (response.getStatusLine().getStatusCode() == 200) {
            //获取响应体
            HttpEntity entity = response.getEntity();
            try {
                //解码响应体(根据网页的编码解码)
                html = EntityUtils.toString(entity,"gbk");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            //关闭发送对象
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return html;
    }
}

 

你可能感兴趣的:(功能代码)