Application2.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package space.anyi;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.nodes.Document;
  4. import org.jsoup.nodes.Element;
  5. import org.jsoup.select.Elements;
  6. import us.codecraft.webmagic.Page;
  7. import us.codecraft.webmagic.Request;
  8. import us.codecraft.webmagic.Site;
  9. import us.codecraft.webmagic.Spider;
  10. import us.codecraft.webmagic.processor.PageProcessor;
  11. import java.sql.*;
  12. import java.time.LocalDate;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. public class Application2 implements PageProcessor {
  16. //初始url
  17. static String indexUrl = "https://www.tjdl.gov.cn/gongkai/zfxxgkzl/zfgbm/dlqmzj/gknr/czyjs/index%s.html";
  18. static int total = 1;
  19. static int index = 0;
  20. public static final String baseUrl = "https://www.tjdl.gov.cn";
  21. public static Pattern p = Pattern.compile("(\\d{4})(年|/|-|\\.)(\\d{1,2})(月|/|-|\\.)(\\d{1,2})");
  22. //配置
  23. private Site site = new Site()
  24. .setTimeOut(100 * 1000) //超时时间
  25. .setRetryTimes(3) //重试请求次数
  26. .setRetrySleepTime(3000) //重试间隔时间 请求头
  27. .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36");
  28. @Override
  29. public void process(Page page) {
  30. String currentUrl = page.getUrl().toString();
  31. if (currentUrl.contains("index")) {
  32. String rawText = page.getRawText();
  33. Pattern pattern = Pattern.compile("var countPage = (\\d+)//共多少页");
  34. Matcher matcher = pattern.matcher(rawText);
  35. if (matcher.find()) {
  36. String totalStr = matcher.group(1);
  37. total = Integer.parseInt(totalStr);
  38. }
  39. Document document = Jsoup.parse(rawText);
  40. Elements eachTags = document.select("div.zfxxgk_zd2.borderTop.zfxxgkzd.border0 ul li:has(a)");
  41. if (eachTags != null && eachTags.size() > 0) {
  42. for (Element eachTag : eachTags) {
  43. //解析详情页url
  44. String id = eachTag.select("a").first().attr("href");
  45. id = id.substring(id.indexOf("./") + 1);
  46. String link = currentUrl.substring(0, currentUrl.lastIndexOf("/")) + id;
  47. String title = eachTag.select("a").first().text().trim();
  48. String dateStr = eachTag.select("b").first().text().trim();
  49. System.out.println(title);
  50. System.out.println(link);
  51. //数据库是否已存在
  52. String querySql = "select DETAIL_LINK from XIN_XI_INFO_TEST where DETAIL_LINK = ?";
  53. try(Connection connection = MyDao.getConnection();
  54. PreparedStatement prepareStatement = connection.prepareStatement(querySql);){
  55. prepareStatement.setString(1,link);
  56. ResultSet resultSet = prepareStatement.executeQuery();
  57. if (resultSet.next()) {
  58. System.out.println("跳过" + link);
  59. resultSet.close();
  60. continue;
  61. }
  62. resultSet.close();
  63. } catch (SQLException e) {
  64. throw new RuntimeException(e);
  65. }
  66. //todo
  67. Request request = new Request(link);
  68. request.putExtra("title",title.substring(2));
  69. request.putExtra("listDate",LocalDate.parse(dateStr));
  70. page.addTargetRequest(request);
  71. }
  72. }
  73. page.addTargetRequest(String.format(indexUrl, "_" + (++index)));
  74. } else {
  75. //处理详情网址
  76. String rawText = page.getRawText();
  77. rawText = rawText.replace("display:none;", "");
  78. Document document = Jsoup.parse(rawText);
  79. Element content = document.select("body > div.bgf7f7f7 > div").first();
  80. //文件url清洗
  81. Elements as = content.select("a");
  82. for (Element a : as) {
  83. String href = a.attr("href");
  84. if (href.startsWith("./")) {
  85. href = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + href.substring(href.indexOf("./") + 1);
  86. a.attr("href", href);
  87. } else if (href.startsWith("../")) {
  88. href = baseUrl + href.substring(href.lastIndexOf("../") + 2);
  89. a.attr("href", href);
  90. } else if (href.startsWith("/")) {
  91. href = baseUrl + href;
  92. a.attr("href", href);
  93. }
  94. }
  95. //图片url清洗
  96. Elements imgs = content.select("img");
  97. for (Element img : imgs) {
  98. String src = img.attr("src");
  99. if (src.startsWith("./")) {
  100. //https://www.tjdl.gov.cn/gongkai/zfxxgkzl/zfgbm/dlqmzj/gknr/czyjs/202507/W020250723358692283424_ORIGIN.jpg
  101. src = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + src.substring(src.indexOf("./") + 1);
  102. img.attr("src", src);
  103. } else if (src.startsWith("../")) {
  104. src = baseUrl + src.substring(src.lastIndexOf("../") + 2);
  105. img.attr("src", src);
  106. } else if (src.startsWith("/")) {
  107. src = baseUrl + src;
  108. img.attr("src", src);
  109. }
  110. }
  111. //头部清洗
  112. content.select("div.dqwz").remove();
  113. content.select("div.contentMemo").remove();
  114. content.select("#articlePlayer").remove();
  115. content.select("#share").remove();
  116. content.select("dl.QrcodeBot").remove();
  117. //底部清洗
  118. content.select("#file_document").remove();
  119. content.select("div.inRight").remove();
  120. content.select("script").remove();
  121. content.select("style").remove();
  122. //save
  123. String SOURCE_NAME = "天津市东丽区民政局";
  124. String DETAIL_LINK = currentUrl;
  125. String DETAIL_TITLE = content.select("h1").first().text().trim();
  126. String DETAIL_CONTENT = content.toString();
  127. LocalDate PAGE_TIME = ((LocalDate) page.getRequest().getExtra("listDate"));
  128. LocalDate CREATE_TIME = LocalDate.now();
  129. String LIST_TITLE = ((String) page.getRequest().getExtra("title"));
  130. String CREATE_BY = "黄略";
  131. String sql = "insert into " + MyDao.TABLE_NAME +"(SOURCE_NAME,DETAIL_LINK,DETAIL_TITLE,DETAIL_CONTENT,PAGE_TIME,CREATE_TIME,LIST_TITLE,CREATE_BY) VALUES (?,?,?,?,?,?,?,?)";
  132. try(Connection connection = MyDao.getConnection();
  133. PreparedStatement preparedStatement = connection.prepareStatement(sql);){
  134. preparedStatement.setString(1, SOURCE_NAME);
  135. preparedStatement.setString(2, DETAIL_LINK);
  136. preparedStatement.setString(3, DETAIL_TITLE);
  137. preparedStatement.setString(4, DETAIL_CONTENT);
  138. preparedStatement.setDate(5, Date.valueOf(PAGE_TIME)); // LocalDate -> java.sql.Date
  139. preparedStatement.setDate(6, Date.valueOf(CREATE_TIME));
  140. preparedStatement.setString(7, LIST_TITLE);
  141. preparedStatement.setString(8, CREATE_BY);
  142. preparedStatement.executeUpdate();
  143. } catch (SQLException e) {
  144. throw new RuntimeException(e);
  145. }
  146. }
  147. }
  148. @Override
  149. public Site getSite() {
  150. return site;
  151. }
  152. //启动
  153. public static void main(String[] args) {
  154. Spider spider = Spider.create(new Application2())
  155. .addUrl("https://www.tjdl.gov.cn/gongkai/zfxxgkzl/zfgbm/dlqmzj/gknr/czyjs/index.html");
  156. System.out.println("start");
  157. spider.run();
  158. System.out.println("end");
  159. }
  160. }