package com.tzld.piaoquan.longarticle.utils; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import java.io.IOException; import java.net.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.UUID; public class HkspSearch { public static List hkspSearch(String key, List sensitiveWords, String traceId) { try { long timestampMilliseconds = System.currentTimeMillis(); String url = "https://haokan.baidu.com/haokan/ui-search/pc/search/video"; String encodedKey = URLEncoder.encode(key, "UTF-8"); String strings = String.format("%d_%s_%d_%d_%d", 1, encodedKey, 10, timestampMilliseconds, 1); String sign = md5(strings); JSONObject params = new JSONObject(); params.put("pn", 1); params.put("rn", 10); params.put("type", "video"); params.put("query", key); params.put("sign", sign); params.put("version", 1); params.put("timestamp", timestampMilliseconds); String base64String = Base64.getEncoder().encodeToString(UUID.randomUUID().toString().getBytes()); // 这行代码是身份验证的关键配置,不然身份验证不起作用 System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); // 身份验证 Authenticator.setDefault( new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "t11983523373311", "mtuhdr2z".toCharArray()); } } ); HttpResponse response = HttpRequest.get(url) .header("authority", "haokan.baidu.com") .header("accept", "*/*") .header("accept-language", "zh,en;q=0.9,zh-CN;q=0.8") .header("cookie", "BIDUPSID=" + base64String) .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0") // 假用户代理 .header("x-requested-with", "xmlhttprequest") .timeout(120000) // 设置超时时间 .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818))) .form(params) .execute(); List resultList = new ArrayList<>(); System.out.println(response.body()); JSONObject jsonResponse = JSONUtil.parseObj(response.body()); List dataList = jsonResponse.getByPath("data.list", List.class); for (JSONObject data : dataList) { try { String videoId = data.getStr("vid"); String title = data.getStr("title"); int duration = parseDuration(data.getStr("duration")); if (sensitiveFlag(sensitiveWords, title) && duration <= 300) { JSONObject res = getVideoDetail(videoId); if (res != null) { resultList.add(res); } } } catch (Exception e) { // 处理异常 } } return resultList; } catch (Exception e) { return new ArrayList<>(); } } private static int parseDuration(String duration) { String[] parts = duration.split(":"); return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]); } private static String md5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : messageDigest) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } private static boolean sensitiveFlag(List sensitiveWords, String title) { // 实现敏感词检查逻辑 return true; // 示例 } public static JSONObject getVideoDetail(String videoId) { String url = "https://haokan.baidu.com/v"; JSONObject params = new JSONObject(); params.put("vid", videoId); params.put("_format", "json"); String base64String = Base64.getEncoder().encodeToString(UUID.randomUUID().toString().getBytes()); // 这行代码是身份验证的关键配置,不然身份验证不起作用 System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); // 身份验证 Authenticator.setDefault( new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "t11983523373311", "mtuhdr2z".toCharArray()); } } ); HttpResponse response = HttpRequest.get(url) .header("Accept", "*/*") .header("cookie", "BIDUPSID=" + base64String) .header("Accept-Language", "en,zh;q=0.9,zh-CN;q=0.8") .header("Cache-Control", "no-cache") .header("Connection", "keep-alive") .header("Content-Type", "application/x-www-form-urlencoded") .header("Referer", "https://haokan.baidu.com") .form(params) .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818))) .execute(); return JSONUtil.parseObj(response.body()).getByPath("data.apiData.curVideoMeta", JSONObject.class); } }