package org.jeecg.modules.stm.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class SpiderUtil  {

    public static void main(String[] args) throws Exception {
        SpiderUtil spiderUtil = new SpiderUtil();
        spiderUtil.spiderStandardData();
    }

    public static JsonArray spiderStandardData() throws Exception {
        String targetUrl = "https://hbba.sacinfo.org.cn/stdQueryList";
        int pages=1;
        int curPage=1;
        JsonArray retAry = new JsonArray();
        do {
            StringBuffer sbUrl = new StringBuffer();
            sbUrl.append(targetUrl).append("?current=").append(curPage++).append("&").append("size=100").append("&key=&ministry=&industry=%E9%87%91%E8%9E%8D&pubdate=&date=&status=%E7%8E%B0%E8%A1%8C");
            URL url = new URL(sbUrl.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");

            // 设置请求头信息
            conn.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Origin", "https://hbba.sacinfo.org.cn");
            conn.setRequestProperty("Referer", "https://hbba.sacinfo.org.cn/stdList?key=&trade=%E9%87%91%E8%9E%8D");
            conn.setRequestProperty("Sec-Fetch-Dest", "empty");
            conn.setRequestProperty("Sec-Fetch-Mode", "cors");
            conn.setRequestProperty("Sec-Fetch-Site", "same-origin");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

            // 读取HTTP响应结果
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            conn.disconnect();

            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(response.toString(), JsonObject.class);
            int rptCode = jsonObject.get("total").getAsInt();
            pages = jsonObject.get("pages").getAsInt();
            JsonArray list = jsonObject.get("records").getAsJsonArray();
            retAry.addAll(list);
        } while (curPage<=pages);
        return retAry;
    }

}

