feat: 研究生成绩获取/解析

refact: 部分jsoup元素操作移至独立的JsoupUtil中
old-package
lensfrex 2 years ago
parent 976b2fd54b
commit 43d3b11fb4
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/api/Graduate.java
  2. 2
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateCourseTableParser.java
  3. 40
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateExamInfoParser.java
  4. 63
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/postgraduate/GraduateStudentInfoPageParser.java
  5. 31
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/undergraduate/ExamInfoParser.java
  6. 4
      mywust-core/src/main/java/cn/linghang/mywust/core/request/graduate/GraduateRequestFactory.java
  7. 57
      mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java

@ -6,4 +6,6 @@ public class Graduate {
public static final String GRADUATE_STUDENT_INFO_API = "http://59.68.177.189/pyxx/grgl/student_info1.aspx"; public static final String GRADUATE_STUDENT_INFO_API = "http://59.68.177.189/pyxx/grgl/student_info1.aspx";
public static final String GRADUATE_COURSE_TABLE_API = "http://59.68.177.189/pyxx/pygl/kbcx_xs.aspx"; public static final String GRADUATE_COURSE_TABLE_API = "http://59.68.177.189/pyxx/pygl/kbcx_xs.aspx";
public static final String GRADUATE_SCORE_API = "http://59.68.177.189/pyxx/grgl/xskccjcx.aspx";
public static final String GRADUATE_TRAINING_PLAN_PAGE_API = "http://59.68.177.189/pyxx/pygl/pyjhcx.aspx";
} }

@ -14,7 +14,7 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class GraduateCourseTablePageParser implements Parser<List<Course>> { public class GraduateCourseTableParser implements Parser<List<Course>> {
private static final Pattern COURSE_TABLE_REGEX = Pattern.compile("课程:(?<name>.*?)<br>班级:(?<class>.*?)<br>\\((?<classRoom>.*?)\\)<br>(?<week>.*?)<br>(?<section>.*?)<br>主讲教师:(?<teacher>.*?)<br>"); private static final Pattern COURSE_TABLE_REGEX = Pattern.compile("课程:(?<name>.*?)<br>班级:(?<class>.*?)<br>\\((?<classRoom>.*?)\\)<br>(?<week>.*?)<br>(?<section>.*?)<br>主讲教师:(?<teacher>.*?)<br>");

@ -0,0 +1,40 @@
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.model.global.ExamInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
public class GraduateExamInfoParser implements Parser<List<ExamInfo>> {
@Override
public List<ExamInfo> parse(String html) throws ParseException {
Document document = Jsoup.parse(html);
Elements scoreElements = document.getElementsByClass("GridViewRowStyle");
List<ExamInfo> examInfos = new ArrayList<>(scoreElements.size());
for (Element scoreElement : scoreElements) {
Elements infoGirds = scoreElement.getElementsByTag("td");
if (infoGirds.size() < 4) {
continue;
}
ExamInfo examInfo = new ExamInfo();
examInfo.setCourseName(infoGirds.get(0).text());
examInfo.setCredit(infoGirds.get(1).text());
examInfo.setTerm(infoGirds.get(2).text());
examInfo.setScore(infoGirds.get(3).text());
examInfos.add(examInfo);
}
return examInfos;
}
}

@ -2,6 +2,7 @@ package cn.linghang.mywust.core.parser.postgraduate;
import cn.linghang.mywust.core.exception.ParseException; import cn.linghang.mywust.core.exception.ParseException;
import cn.linghang.mywust.core.parser.Parser; import cn.linghang.mywust.core.parser.Parser;
import cn.linghang.mywust.core.util.JsoupUtil;
import cn.linghang.mywust.model.global.StudentInfo; import cn.linghang.mywust.model.global.StudentInfo;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -17,81 +18,39 @@ public class GraduateStudentInfoPageParser implements Parser<StudentInfo> {
StudentInfo student = new StudentInfo(); StudentInfo student = new StudentInfo();
Element studentNumberElement = table.getElementById("txtxh"); Element studentNumberElement = table.getElementById("txtxh");
student.setStudentNumber(this.getAttr(studentNumberElement, "value")); student.setStudentNumber(JsoupUtil.getAttr(studentNumberElement, "value"));
// Element gradeElement = table.getElementById("txtnj"); // Element gradeElement = table.getElementById("txtnj");
// int grade = Integer.parseInt(this.getAttr(gradeElement, "value")); // int grade = Integer.parseInt(JsoupUtil.getAttr(gradeElement, "value"));
Element idNumberElement = table.getElementById("txtsfzh"); Element idNumberElement = table.getElementById("txtsfzh");
student.setIdNumber(this.getAttr(idNumberElement, "value")); student.setIdNumber(JsoupUtil.getAttr(idNumberElement, "value"));
Element nationalityElement = table.getElementById("drpmz"); Element nationalityElement = table.getElementById("drpmz");
student.setNationality(this.getSelectContent(nationalityElement)); student.setNationality(JsoupUtil.getSelectContent(nationalityElement));
Element hometownElement = table.getElementById("txtjg"); Element hometownElement = table.getElementById("txtjg");
student.setHometown(this.getAttr(hometownElement, "value")); student.setHometown(JsoupUtil.getAttr(hometownElement, "value"));
Element nameElement = table.getElementById("txtxm"); Element nameElement = table.getElementById("txtxm");
student.setName(this.getAttr(nameElement, "value")); student.setName(JsoupUtil.getAttr(nameElement, "value"));
Element birthdayElement = table.getElementById("lblcsrq"); Element birthdayElement = table.getElementById("lblcsrq");
student.setBirthday(this.getText(birthdayElement)); student.setBirthday(JsoupUtil.getText(birthdayElement));
Element collegeElement = table.getElementById("droyx"); Element collegeElement = table.getElementById("droyx");
student.setCollege(this.getSelectContent(collegeElement)); student.setCollege(JsoupUtil.getSelectContent(collegeElement));
Element majorElement = table.getElementById("drozy"); Element majorElement = table.getElementById("drozy");
student.setMajor(this.getSelectContent(majorElement)); student.setMajor(JsoupUtil.getSelectContent(majorElement));
Element genderElement = table.getElementById("droxb"); Element genderElement = table.getElementById("droxb");
student.setSex(this.getSelectContent(genderElement)); student.setSex(JsoupUtil.getSelectContent(genderElement));
// 研究生没有班级 // 研究生没有班级
student.setClazz(""); student.setClazz("");
return student; return student;
} }
/**
* 从Element中拿到指定的标签值
* @param element 元素对象
* @param key 标签值的key
* @return 相应的值若element为空则返回空字符串
*/
private String getAttr(Element element, String key) {
if (element == null) {
return "";
} else {
return element.attr(key);
}
}
/**
* 从Element中拿到指定的文本内容
* @param element 元素对象
* @return 相应的值若element为空则返回空字符串
*/
public String getText(Element element) {
if (element == null) {
return "";
} else {
return element.ownText();
}
}
/**
* 从select类型的Element中拿取到已选中的选项值
* @param element 元素对象
* @return 相应的值若element为空则返回空字符串
*/
public String getSelectContent(Element element) {
if (element == null) {
return "";
} else {
return element.getElementsByAttributeValue("selected", "selected")
.get(0)
.ownText();
}
}
} }

@ -3,6 +3,7 @@ package cn.linghang.mywust.core.parser.undergraduate;
import cn.linghang.mywust.core.exception.ParseException; import cn.linghang.mywust.core.exception.ParseException;
import cn.linghang.mywust.core.parser.Parser; import cn.linghang.mywust.core.parser.Parser;
import cn.linghang.mywust.core.parser.undergraduate.xpath.ExamInfoXpath; import cn.linghang.mywust.core.parser.undergraduate.xpath.ExamInfoXpath;
import cn.linghang.mywust.core.util.JsoupUtil;
import cn.linghang.mywust.model.global.ExamInfo; import cn.linghang.mywust.model.global.ExamInfo;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -38,19 +39,23 @@ public class ExamInfoParser implements Parser<List<ExamInfo>> {
ExamInfo examInfo = new ExamInfo(); ExamInfo examInfo = new ExamInfo();
// 这段看着震撼,但其实很丑 // 这段看着震撼,但其实很丑
examInfo.setId(girds.get(0).text()); examInfo.setId(JsoupUtil.getElementContext(girds.get(0)));
examInfo.setTerm(girds.get(1).text());
examInfo.setCourseNumber(girds.get(2).text()); examInfo.setTerm(JsoupUtil.getElementContext(girds.get(1)));
examInfo.setCourseName(girds.get(3).text()); examInfo.setCourseNumber(JsoupUtil.getElementContext(girds.get(2)));
examInfo.setGroupName(girds.get(4).text());
examInfo.setScore(girds.get(5).text()); examInfo.setCourseName(JsoupUtil.getElementContext(girds.get(3)));
examInfo.setFlag(girds.get(6).text()); examInfo.setGroupName(JsoupUtil.getElementContext(girds.get(4)));
examInfo.setCredit(girds.get(7).text());
examInfo.setCourseHours(girds.get(8).text()); examInfo.setScore(JsoupUtil.getElementContext(girds.get(5)));
examInfo.setGradePoint(girds.get(9).text()); examInfo.setFlag(JsoupUtil.getElementContext(girds.get(6)));
examInfo.setEvaluateMethod(girds.get(11).text()); examInfo.setCredit(JsoupUtil.getElementContext(girds.get(7)));
examInfo.setKind(girds.get(12).text()); examInfo.setCourseHours(JsoupUtil.getElementContext(girds.get(8)));
examInfo.setCourseKind(girds.get(13).text()); examInfo.setGradePoint(JsoupUtil.getElementContext(girds.get(9)));
examInfo.setEvaluateMethod(JsoupUtil.getElementContext(girds.get(11)));
examInfo.setKind(JsoupUtil.getElementContext(girds.get(12)));
examInfo.setCourseKind(JsoupUtil.getElementContext(girds.get(13)));
examInfos.add(examInfo); examInfos.add(examInfo);
} }

@ -43,4 +43,8 @@ public class GraduateRequestFactory extends RequestFactory {
public static HttpRequest courseTableRequest(String cookie) { public static HttpRequest courseTableRequest(String cookie) {
return makeHttpRequest(Graduate.GRADUATE_COURSE_TABLE_API, null, cookie); return makeHttpRequest(Graduate.GRADUATE_COURSE_TABLE_API, null, cookie);
} }
public static HttpRequest examScoreInfoRequest(String cookie) {
return makeHttpRequest(Graduate.GRADUATE_SCORE_API, null, cookie);
}
} }

@ -0,0 +1,57 @@
package cn.linghang.mywust.core.util;
import org.jsoup.nodes.Element;
public class JsoupUtil {
/**
* 获取Element对象中的内容(text)用途是对null判断防止直接.text()产生空指针错误
*
* @param element 元素对象
* @return 获取到的内容如果element为null则返回一个空的字符串
*/
public static String getElementContext(Element element) {
return element == null ? "" : element.text();
}
/**
* 从Element中拿到指定的标签值
* @param element 元素对象
* @param key 标签值的key
* @return 相应的值若element为空则返回空字符串
*/
public static String getAttr(Element element, String key) {
if (element == null) {
return "";
} else {
return element.attr(key);
}
}
/**
* 从Element中拿到指定的文本内容
* @param element 元素对象
* @return 相应的值若element为空则返回空字符串
*/
public static String getText(Element element) {
if (element == null) {
return "";
} else {
return element.ownText();
}
}
/**
* 从select类型的Element中拿取到已选中的选项值
* @param element 元素对象
* @return 相应的值若element为空则返回空字符串
*/
public static String getSelectContent(Element element) {
if (element == null) {
return "";
} else {
return element.getElementsByAttributeValue("selected", "selected")
.get(0)
.ownText();
}
}
}
Loading…
Cancel
Save