使用HttpClient爬取数据

package com.ruoyi.web.controller;


import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

public class MyBookBug {

    private static String URL = "";


    public static void getData(String url) throws IOException {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(50000).setConnectionRequestTimeout(10000).setSocketTimeout(50000)
                .build();
        BasicCookieStore cookieStore = new BasicCookieStore();
        List<Cookie> cookies = new ArrayList<>();
        if (!CollectionUtils.isEmpty(cookies)) {
            for (Cookie c : cookies) {
                cookieStore.addCookie(c);
            }
        }
        HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultRequestConfig(config);
        if (cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(cookieStore);
        }
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpResponse response;
        try {
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                String html = EntityUtils.toString(entity, Consts.UTF_8);
                Document doc = null;
                doc = Jsoup.parse(html);
//                System.out.println(doc);
                Elements p = doc.select(".read-content").select("p");
                System.out.println(p);

                String href = doc.select("#j_chapterNext").attr("href");
                if (StringUtils.hasText(href)) {
                    if (!href.contains("https:")) {
                        href = "https:" + href;
                    }
                    getData(href);
                }
                EntityUtils.consume(entity);
            }
            // 获取cookies信息
        } catch (Exception e) {
        } finally {
            httpClient.close();
        }
    }


    public static void main(String[] args) throws IOException {
        getData(URL);
    }

}

Logo

快速构建 Web 应用程序

更多推荐