You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
12 KiB

1 month ago
  1. package com.example;
  2. import okhttp3.*;
  3. import org.json.JSONObject;
  4. import org.jsoup.Jsoup;
  5. import org.jsoup.nodes.Document;
  6. import org.jsoup.nodes.Element;
  7. import org.jsoup.select.Elements;
  8. import java.io.IOException;
  9. import java.net.InetSocketAddress;
  10. import java.net.Proxy;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.time.LocalDate;
  14. import java.time.LocalDateTime;
  15. import java.time.ZonedDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.time.format.DateTimeParseException;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import java.util.concurrent.TimeUnit;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. public class ook {
  26. public static void main(String[] args) throws Exception {
  27. // 1. 获取代理地址
  28. // String proxyJson = getProxyFromLocalService();
  29. // JSONObject proxyData = new JSONObject(proxyJson);
  30. // String httpProxy = proxyData.getString("http"); // 例如 "http://proxy1:port"
  31. //
  32. // // 2. 解析代理地址
  33. // String[] proxyParts = httpProxy.replace("http://", "").split(":");
  34. // String proxyHost = proxyParts[0]; // proxy1
  35. // int proxyPort = Integer.parseInt(proxyParts[1]); // port
  36. OkHttpClient client = new OkHttpClient().newBuilder()
  37. .connectTimeout(30, TimeUnit.SECONDS)
  38. .readTimeout(30, TimeUnit.SECONDS)
  39. .writeTimeout(30, TimeUnit.SECONDS)
  40. .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7897))) // 直接使用 7897 端口
  41. .build();
  42. MediaType mediaType = MediaType.parse("text/plain");
  43. Request request = new Request.Builder()
  44. .url("https://wrair.health.mil/News-Media/Press-Releases/")
  45. .get()
  46. // 添加关键请求头
  47. .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
  48. .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
  49. // .addHeader("Accept-Encoding", "gzip, deflate, br, zstd")
  50. .addHeader("Accept-Language", "zh-CN,zh;q=0.9,th;q=0.8")
  51. .addHeader("Cache-Control", "no-cache")
  52. .addHeader("Pragma", "no-cache")
  53. .addHeader("Referer", "https://wrair.health.mil/News-Media/Press-Releases/")
  54. .addHeader("Cookie", "_ga=GA1.1.516170455.1740971326; .ASPXANONYMOUS=xUBztj4Ek1vHfBPe-1QqFJhd83I4bkB1k0_d-2QrQ7drfd7R7Y6eNsyyHVjSeffyIKzy_qm5tOKOCtbvst-s9ZGWThxifCGMdJE117EQlr1OZARa0; dnn_IsMobile=False; language=en-US; ARRAffinity=c30f7cdebcf208f7c5a996cb410451c36532afc64703669607f68f04a75f4b39; _ga_CSLL4ZEK4L=GS1.1.1742349582.4.1.1742350035.0.0.0")
  55. .addHeader("Upgrade-Insecure-Requests", "1")
  56. .addHeader("Sec-Fetch-Dest", "document")
  57. .addHeader("Sec-Fetch-Mode", "navigate")
  58. .addHeader("Sec-Fetch-Site", "same-origin")
  59. .addHeader("Sec-Fetch-User", "?1")
  60. .addHeader("Sec-Ch-Ua", "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Google Chrome\";v=\"134\"")
  61. .addHeader("Sec-Ch-Ua-Mobile", "?0")
  62. .addHeader("Sec-Ch-Ua-Platform", "\"Windows\"")
  63. .addHeader("Priority", "u=0, i")
  64. .build();
  65. Response response = client.newCall(request).execute();
  66. String html = response.body().string();
  67. Document parse = Jsoup.parse(html);
  68. // String url = "https://www.uu.se/nyheter/alla?newsResearch=researchtopic11%3Bresearchtopic7%3Bresearchtopic22%3Bresearchtopic10%3Bresearchtopic2&start=20";
  69. // // 定义正则表达式
  70. // String regex = "start=(\\d+)";
  71. // Pattern pattern = Pattern.compile(regex);
  72. // Matcher matcher = pattern.matcher(url);
  73. // Integer start = 0;
  74. // String postTime = convertToTimestamp(parse.select(".mr10").text());
  75. // String title = parse.select(".hdg01").text();
  76. // String content = parse.select(".container01 p").text();
  77. // String forwardcontent = parse.select("#main").html();
  78. // Map<String,Object> map = new HashMap<>();
  79. // if (matcher.find()) {
  80. // start = Integer.parseInt(matcher.group(1));
  81. // System.out.println("Start: " + start); // start = 12
  82. // }
  83. //
  84. // Elements allLinks = new Elements();
  85. // Elements links = parse.select(".search-result-hit-text-container a");
  86. // allLinks.addAll(links);
  87. //
  88. // int totalLinks = allLinks.size();
  89. // int startIndex = Math.max(0, totalLinks - 10);
  90. // for (int i = startIndex; i < totalLinks; i++) {
  91. // Map<String, Object> task = new HashMap<String, Object>(16);
  92. // task.put("link","https://www.uu.se"+allLinks.get(i).attr("href"));
  93. // task.put("linktype", "newscontent"); // 設置鏈接類型為 "newscontent"
  94. //
  95. // System.out.println(task);
  96. // }
  97. Elements elements = parse.select(".title a");
  98. for (Element element : elements) {
  99. String link = element.attr("href");
  100. System.out.println(link);
  101. }
  102. // map.put("postTime",postTime);
  103. // map.put("title",title);
  104. // map.put("content",content);
  105. // map.put("forwardcontent",forwardcontent);
  106. // System.out.println(map);
  107. }
  108. public ook() throws IOException {
  109. }
  110. // public static String convertToTimestamp(String dateStr) {
  111. // try {
  112. // // 定义输入格式:dd MMMM , yyyy(例如 "28 February , 2025")
  113. // DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.ENGLISH);
  114. // // 定义输出格式:yyyy-MM-dd HH:mm:ss
  115. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  116. //
  117. // // 解析输入日期
  118. // LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  119. // // 转换为带时间的格式,时间设为 00:00:00
  120. // return date.atStartOfDay().format(outputFormatter);
  121. // } catch (Exception e) {
  122. // e.printStackTrace();
  123. // return null; // 或抛出异常,根据需求调整
  124. // }
  125. // }
  126. // public static String convertToTimestamp(String dateStr) {
  127. // try {
  128. // // 定义输入格式:yyyy 年 MM 月 dd 日
  129. // DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.CHINESE);
  130. // // 定义输出格式:yyyy-MM-dd HH:mm:ss
  131. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  132. //
  133. // // 解析输入日期
  134. // LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  135. // // 转换为带时间的格式,时间设为 00:00:00
  136. // return date.atStartOfDay().format(outputFormatter);
  137. // } catch (Exception e) {
  138. // e.printStackTrace();
  139. // return null; // 或抛出异常,根据需求调整
  140. // }
  141. // }
  142. // public static String convertToTimestamp(String dateStr) {
  143. // try {
  144. // // 定义输入格式
  145. // DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  146. // // 定义输出格式
  147. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  148. //
  149. // // 解析输入字符串为 LocalDate
  150. // LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  151. // // 转换为 LocalDateTime,设置时间为 00:00:00
  152. // LocalDateTime dateTime = date.atStartOfDay();
  153. // // 格式化为目标字符串
  154. // return dateTime.format(outputFormatter);
  155. // } catch (Exception e) {
  156. // e.printStackTrace();
  157. // return null; // 或者抛出异常,根据需求调整
  158. // }
  159. // }
  160. // public static String convertToTimestamp(String dateStr) {
  161. // try {
  162. // // 定义输入格式:MMMM d, yyyy(例如 "June 3, 2015")
  163. // DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("d MMMM, yyyy", Locale.ENGLISH);
  164. // // 定义输出格式:yyyy-MM-dd HH:mm:ss
  165. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  166. //
  167. // // 解析输入日期
  168. // LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  169. // // 转换为带时间的格式,时间设为 00:00:00
  170. // return date.atStartOfDay().format(outputFormatter);
  171. // } catch (Exception e) {
  172. // e.printStackTrace();
  173. // return null; // 或抛出异常,根据需求调整
  174. // }
  175. // }
  176. // public static String convertToTimestamp(String input) {
  177. // try {
  178. // // 正则匹配 "d MMMM yyyy"
  179. // Pattern pattern = Pattern.compile("\\d{1,2} [A-Za-z]+ \\d{4}");
  180. // Matcher matcher = pattern.matcher(input);
  181. // if (matcher.find()) {
  182. // String dateStr = matcher.group();
  183. // DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.ENGLISH);
  184. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  185. // LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  186. // return date.atStartOfDay().format(outputFormatter);
  187. // } else {
  188. // System.out.println("No date found in: " + input);
  189. // return null;
  190. // }
  191. // } catch (Exception e) {
  192. // e.printStackTrace();
  193. // return null;
  194. // }
  195. // }
  196. // public static String convertToTimestamp(String dateStr) {
  197. // try {
  198. // // Parse the ISO 8601 date string (e.g., "2025-03-17T12:37:33.033Z")
  199. // ZonedDateTime zdt = ZonedDateTime.parse(dateStr, DateTimeFormatter.ISO_DATE_TIME);
  200. //
  201. // // Define the output format (yyyy-MM-dd hh:mm:ss)
  202. // DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  203. //
  204. // // Format the date to the desired output
  205. // return zdt.format(outputFormatter);
  206. // } catch (Exception e) {
  207. // e.printStackTrace();
  208. // return null; // Or throw an exception, depending on your needs
  209. // }
  210. // }
  211. public static String convertToTimestamp(String dateStr) {
  212. try {
  213. // Parse "Jan. 9, 2025" (abbreviated month, dot, comma-separated)
  214. DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("MMM. d, yyyy", Locale.ENGLISH);
  215. LocalDate date = LocalDate.parse(dateStr, inputFormatter);
  216. // Format to "yyyy-MM-dd HH:mm:ss" (defaulting time to 00:00:00)
  217. DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  218. return date.atStartOfDay().format(outputFormatter);
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. return null;
  222. }
  223. }
  224. // 调用本地代理服务获取代理地址
  225. private static String getProxyFromLocalService() throws Exception {
  226. OkHttpClient client = new OkHttpClient();
  227. Request request = new Request.Builder()
  228. .url("http://127.0.0.1:7897")
  229. .get()
  230. .build();
  231. try (Response response = client.newCall(request).execute()) {
  232. if (response.isSuccessful()) {
  233. return response.body().string(); // 返回 JSON 字符串
  234. } else {
  235. throw new Exception("获取代理失败,状态码: " + response.code());
  236. }
  237. }
  238. }
  239. }