From 2e80bcb99cb78a685a62a25430ecf0b0771589c0 Mon Sep 17 00:00:00 2001 From: lensferno Date: Tue, 22 Nov 2022 21:40:54 +0800 Subject: [PATCH] =?UTF-8?q?impl:=20=E7=A0=94=E7=A9=B6=E7=94=9F=E5=9F=B9?= =?UTF-8?q?=E5=85=BB=E6=96=B9=E6=A1=88=E8=8E=B7=E5=8F=96/=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postgraduate/GraduateExamInfoParser.java | 2 ++ .../GraduateTrainingPlanPageParser.java | 34 +++++++++++++++++++ .../graduate/GraduateRequestFactory.java | 4 +++ .../linghang/mywust/core/util/JsoupUtil.java | 9 ++++- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateTrainingPlanPageParser.java diff --git a/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java b/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java index 1a9b92e..d37b16d 100644 --- a/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java +++ b/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java @@ -25,6 +25,8 @@ public class GraduateExamInfoParser implements Parser> { continue; } + scoreElements.removeIf(element -> element.hasClass("pagestopr")); + ExamInfo examInfo = new ExamInfo(); examInfo.setCourseName(infoGirds.get(0).text()); diff --git a/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateTrainingPlanPageParser.java b/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateTrainingPlanPageParser.java new file mode 100644 index 0000000..2dc4988 --- /dev/null +++ b/mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateTrainingPlanPageParser.java @@ -0,0 +1,34 @@ +package cn.linghang.mywust.core.parser.postgraduate; + +import cn.linghang.mywust.core.exception.ParseException; +import cn.linghang.mywust.core.parser.Parser; +import cn.linghang.mywust.core.util.JsoupUtil; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +public class GraduateTrainingPlanPageParser implements Parser { + + private static final String HTML_PREFIX = "培养计划"; + private static final String HTML_SUFFIX = ""; + + @Override + public String parse(String html) throws ParseException { + try { + Document document = Jsoup.parse(html); + Element fullTable = document.getElementById("Fin_Context"); + if (fullTable == null) { + throw new ParseException("解析研究生培养计划失败,元素Fin_Context不存在", html); + } + + String head = fullTable.getElementsByClass("pagestopl").get(0).outerHtml(); + String table = JsoupUtil.getOuterHtml(fullTable.getElementById("_ctl0_MainWork_dgData")); + String foot = JsoupUtil.getOuterHtml(fullTable.getElementById("Table4")); + + return HTML_PREFIX + head + table + foot + HTML_SUFFIX; + }catch (Exception e) { + // 解析失败就直接返回原网页展示 + return html; + } + } +} diff --git a/mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java b/mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java index 7091361..a24700c 100644 --- a/mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java +++ b/mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java @@ -47,4 +47,8 @@ public class GraduateRequestFactory extends RequestFactory { public static HttpRequest examScoreInfoRequest(String cookie) { return makeHttpRequest(Graduate.GRADUATE_SCORE_API, null, cookie); } + + public static HttpRequest trainingPlanPageRequest(String cookie) { + return makeHttpRequest(Graduate.GRADUATE_TRAINING_PLAN_PAGE_API, null, cookie); + } } diff --git a/mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java b/mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java index 9711452..d925470 100644 --- a/mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java +++ b/mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java @@ -13,10 +13,15 @@ public class JsoupUtil { return element == null ? "" : element.text(); } + public static String getOuterHtml(Element element) { + return element == null ? "" : element.outerHtml(); + } + /** * 从Element中拿到指定的标签值 + * * @param element 元素对象 - * @param key 标签值的key + * @param key 标签值的key * @return 相应的值,若element为空则返回空字符串 */ public static String getAttr(Element element, String key) { @@ -29,6 +34,7 @@ public class JsoupUtil { /** * 从Element中拿到指定的文本内容 + * * @param element 元素对象 * @return 相应的值,若element为空则返回空字符串 */ @@ -42,6 +48,7 @@ public class JsoupUtil { /** * 从select类型的Element中拿取到已选中的选项值 + * * @param element 元素对象 * @return 相应的值,若element为空则返回空字符串 */