impl: 研究生培养方案获取/解析

old-package
lensfrex 2 years ago
parent 333ee21aa6
commit 2e80bcb99c
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java
  2. 34
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateTrainingPlanPageParser.java
  3. 4
      mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java
  4. 7
      mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java

@ -25,6 +25,8 @@ public class GraduateExamInfoParser implements Parser<List<ExamInfo>> {
continue; continue;
} }
scoreElements.removeIf(element -> element.hasClass("pagestopr"));
ExamInfo examInfo = new ExamInfo(); ExamInfo examInfo = new ExamInfo();
examInfo.setCourseName(infoGirds.get(0).text()); examInfo.setCourseName(infoGirds.get(0).text());

@ -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<String> {
private static final String HTML_PREFIX = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>培养计划</title></head><body>";
private static final String HTML_SUFFIX = "</body></html>";
@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;
}
}
}

@ -47,4 +47,8 @@ public class GraduateRequestFactory extends RequestFactory {
public static HttpRequest examScoreInfoRequest(String cookie) { public static HttpRequest examScoreInfoRequest(String cookie) {
return makeHttpRequest(Graduate.GRADUATE_SCORE_API, null, 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);
}
} }

@ -13,8 +13,13 @@ public class JsoupUtil {
return element == null ? "" : element.text(); return element == null ? "" : element.text();
} }
public static String getOuterHtml(Element element) {
return element == null ? "" : element.outerHtml();
}
/** /**
* 从Element中拿到指定的标签值 * 从Element中拿到指定的标签值
*
* @param element 元素对象 * @param element 元素对象
* @param key 标签值的key * @param key 标签值的key
* @return 相应的值若element为空则返回空字符串 * @return 相应的值若element为空则返回空字符串
@ -29,6 +34,7 @@ public class JsoupUtil {
/** /**
* 从Element中拿到指定的文本内容 * 从Element中拿到指定的文本内容
*
* @param element 元素对象 * @param element 元素对象
* @return 相应的值若element为空则返回空字符串 * @return 相应的值若element为空则返回空字符串
*/ */
@ -42,6 +48,7 @@ public class JsoupUtil {
/** /**
* 从select类型的Element中拿取到已选中的选项值 * 从select类型的Element中拿取到已选中的选项值
*
* @param element 元素对象 * @param element 元素对象
* @return 相应的值若element为空则返回空字符串 * @return 相应的值若element为空则返回空字符串
*/ */

Loading…
Cancel
Save