Application.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package space.anyi;
  2. import org.apache.http.client.config.RequestConfig;
  3. import org.apache.http.client.methods.CloseableHttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.impl.client.CloseableHttpClient;
  6. import org.apache.http.impl.client.HttpClients;
  7. import org.apache.http.util.EntityUtils;
  8. import org.jsoup.Jsoup;
  9. import org.jsoup.nodes.Document;
  10. import org.jsoup.nodes.Element;
  11. import org.jsoup.select.Elements;
  12. import java.io.*;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. public class Application {
  18. static String indexUrl = "https://www.tjdl.gov.cn/gongkai/zfxxgkzl/zfgbm/dlqmzj/gknr/czyjs/index%s.html";
  19. static String baseUrl = "https://www.tjdl.gov.cn";
  20. public static void main(String[] args) throws Exception {
  21. // String res = doGet(listUrl);
  22. // System.out.println(res);
  23. getList();
  24. }
  25. static int total = 1;
  26. static int index = 0;
  27. static int count = 0;
  28. public static void getList() throws Exception {
  29. while (index< total){
  30. String listUrl = index == 0 ? String.format(indexUrl, "") : String.format(indexUrl, "_" + index);
  31. System.out.println(listUrl);
  32. String res = doGet(listUrl);
  33. //var countPage = 5//共多少页
  34. Pattern pattern = Pattern.compile("var countPage = (\\d+)//共多少页");
  35. Matcher matcher = pattern.matcher(res);
  36. if (matcher.find()) {
  37. String totalStr = matcher.group(1);
  38. total = Integer.parseInt(totalStr);
  39. }
  40. Document doc = Jsoup.parse(res);
  41. // Elements eachTags = doc.select("div[class=news-items] ul li:has(a)");
  42. //#xxgk_right > div.zfxxgk_zd2.borderTop.zfxxgkzd.border0
  43. Elements eachTags = doc.select("div.zfxxgk_zd2.borderTop.zfxxgkzd.border0 ul li:has(a)");
  44. if (eachTags != null) {
  45. if (eachTags.size() > 0) {
  46. for (Element eachTag : eachTags) {
  47. count++;
  48. String id = eachTag.select("a").first().attr("href");
  49. id = id.substring(id.indexOf("./") + 1);
  50. String link = listUrl.substring(0, listUrl.lastIndexOf("/")) + id;
  51. String title = eachTag.select("a").first().text().trim();
  52. System.out.println(title);
  53. System.out.println(link);
  54. String detail = doGet(link);
  55. detail = detail.replace("display:none;", "");
  56. Document document = Jsoup.parse(detail);
  57. //body > div.bgf7f7f7 > div > div.listCon > div > div.contentCons > div.pages_content
  58. //body > div.bgf7f7f7 > div
  59. Element content = document.select("body > div.bgf7f7f7 > div").first();
  60. // Element pageContent = content.select("div.pages_content").first();
  61. //文件
  62. Elements as = content.select("a");
  63. for (Element a : as) {
  64. String href = a.attr("href");
  65. if (href.startsWith("./")) {
  66. href = link.substring(0, link.lastIndexOf('/')) + href.substring(href.indexOf("./") + 1);
  67. a.attr("href", href);
  68. } else if (href.startsWith("../")) {
  69. href = baseUrl + href.substring(href.lastIndexOf("../") + 2);
  70. a.attr("href", href);
  71. } else if (href.startsWith("/")) {
  72. href = baseUrl + href;
  73. a.attr("href", href);
  74. }
  75. // String fileTitle = a.text();
  76. // System.out.println(fileTitle);
  77. }
  78. //图片
  79. Elements imgs = content.select("img");
  80. for (Element img : imgs) {
  81. String src = img.attr("src");
  82. if (src.startsWith("./")) {
  83. //https://www.tjdl.gov.cn/gongkai/zfxxgkzl/zfgbm/dlqmzj/gknr/czyjs/202507/W020250723358692283424_ORIGIN.jpg
  84. src = link.substring(0, link.lastIndexOf('/')) + src.substring(src.indexOf("./") + 1);
  85. img.attr("src", src);
  86. } else if (src.startsWith("../")) {
  87. src = baseUrl + src.substring(src.lastIndexOf("../") + 2);
  88. img.attr("src", src);
  89. } else if (src.startsWith("/")) {
  90. src = baseUrl + src;
  91. img.attr("src", src);
  92. }
  93. }
  94. //头部
  95. content.select("div.dqwz").remove();
  96. content.select("div.contentMemo").remove();
  97. content.select("#articlePlayer").remove();
  98. content.select("#share").remove();
  99. content.select("dl.QrcodeBot").remove();
  100. //底部
  101. content.select("#file_document").remove();
  102. content.select("div.inRight").remove();
  103. content.select("script").remove();
  104. content.select("style").remove();
  105. String text = content.text();
  106. //save
  107. try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(System.getProperty("user.dir") + File.separator + "data" + File.separator + title.substring(2) + ".html"))))) {
  108. bufferedWriter.write(content.toString());
  109. }
  110. }
  111. }
  112. }
  113. index++;
  114. }
  115. System.out.println(count);
  116. }
  117. private static int getTotal(String res) {
  118. //var countPage = 5//共多少页
  119. Pattern pattern = Pattern.compile("var countPage = (\\d+)//共多少页");
  120. Matcher matcher = pattern.matcher(res);
  121. if (matcher.find()) {
  122. String totalStr = matcher.group(1);
  123. return Integer.parseInt(totalStr);
  124. }
  125. return total;
  126. }
  127. public static String doGet(String url) throws Exception {
  128. String result = "";
  129. CloseableHttpClient httpClient = HttpClients.createDefault();
  130. HttpGet httpGet = new HttpGet(url);
  131. RequestConfig requestConfig = RequestConfig.custom()
  132. .setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000)
  133. .build();
  134. httpGet.setConfig(requestConfig);
  135. CloseableHttpResponse response = null;
  136. try {
  137. response = httpClient.execute(httpGet);
  138. if (response.getStatusLine().getStatusCode() == 200) {
  139. String content = EntityUtils.toString(response.getEntity(), "UTF-8");
  140. result = content;
  141. }
  142. } finally {
  143. if (response != null) {
  144. response.close();
  145. }
  146. httpClient.hashCode();
  147. }
  148. return result;
  149. }
  150. }