优化本科生学生信息解析处理方式;移除部分不必要的依赖

old-package
lensfrex 2 years ago
parent 24c23c33e1
commit 950e3ca948
Signed by: lensfrex
GPG Key ID: 947ADABD8533C476
  1. 21
      mywust-core/src/main/java/cn/linghang/mywust/core/parser/undergraduate/UndergradStudentInfoPageParser.java
  2. 25
      mywust-core/src/main/java/cn/linghang/mywust/core/util/JsoupUtil.java
  3. 16
      mywust-network-okhttp/pom.xml

@ -2,6 +2,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.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.Document; import org.jsoup.nodes.Document;
@ -18,34 +19,34 @@ public class UndergradStudentInfoPageParser implements Parser<StudentInfo> {
} }
Elements studentElements = table.selectXpath(StudentInfoXpath.STUDENT_NUMBER); Elements studentElements = table.selectXpath(StudentInfoXpath.STUDENT_NUMBER);
String studentNumber = studentElements.isEmpty() ? null : studentElements.get(0).text().replace("学号:", ""); String studentNumber = JsoupUtil.getElementText(studentElements).replace("学号:", "");
Elements collegeElements = table.selectXpath(StudentInfoXpath.COLLEGE); Elements collegeElements = table.selectXpath(StudentInfoXpath.COLLEGE);
String college = collegeElements.isEmpty() ? null : collegeElements.get(0).text().replace("院系:", ""); String college = JsoupUtil.getElementText(collegeElements).replace("院系:", "");
Elements majorElements = table.selectXpath(StudentInfoXpath.MAJOR); Elements majorElements = table.selectXpath(StudentInfoXpath.MAJOR);
String major = majorElements.isEmpty() ? null : majorElements.get(0).text().replace("专业:", ""); String major = JsoupUtil.getElementText(majorElements).replace("专业:", "");
Elements classElements = table.selectXpath(StudentInfoXpath.CLASS); Elements classElements = table.selectXpath(StudentInfoXpath.CLASS);
String clazz = classElements.isEmpty() ? null : classElements.get(0).text().replace("班级:", ""); String clazz = JsoupUtil.getElementText(classElements).replace("班级:", "");
Elements nameElements = table.selectXpath(StudentInfoXpath.NAME); Elements nameElements = table.selectXpath(StudentInfoXpath.NAME);
String name = nameElements.isEmpty() ? null : nameElements.get(0).text(); String name = JsoupUtil.getElementText(nameElements);
Elements sexElements = table.selectXpath(StudentInfoXpath.SEX); Elements sexElements = table.selectXpath(StudentInfoXpath.SEX);
String sex = sexElements.isEmpty() ? null : sexElements.get(0).text(); String sex = JsoupUtil.getElementText(sexElements);
Elements birthdayElements = table.selectXpath(StudentInfoXpath.BIRTHDAY); Elements birthdayElements = table.selectXpath(StudentInfoXpath.BIRTHDAY);
String birthday = birthdayElements.isEmpty() ? null : birthdayElements.get(0).text(); String birthday = JsoupUtil.getElementText(birthdayElements);
Elements hometownElements = table.selectXpath(StudentInfoXpath.HOMETOWN); Elements hometownElements = table.selectXpath(StudentInfoXpath.HOMETOWN);
String hometown = hometownElements.isEmpty() ? null : hometownElements.get(0).text(); String hometown = JsoupUtil.getElementText(hometownElements);
Elements nationalityElements = table.selectXpath(StudentInfoXpath.NATIONALITY); Elements nationalityElements = table.selectXpath(StudentInfoXpath.NATIONALITY);
String nationality = nationalityElements.isEmpty() ? null : nationalityElements.get(0).text(); String nationality = JsoupUtil.getElementText(nationalityElements);
Elements idNumberElements = table.selectXpath(StudentInfoXpath.ID_NUMBER); Elements idNumberElements = table.selectXpath(StudentInfoXpath.ID_NUMBER);
String idNumber = idNumberElements.isEmpty() ? null : idNumberElements.get(0).text(); String idNumber = JsoupUtil.getElementText(idNumberElements);
return StudentInfo.builder() return StudentInfo.builder()
.studentNumber(studentNumber) .studentNumber(studentNumber)

@ -1,6 +1,7 @@
package cn.linghang.mywust.core.util; package cn.linghang.mywust.core.util;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupUtil { public class JsoupUtil {
/** /**
@ -61,4 +62,28 @@ public class JsoupUtil {
.ownText(); .ownText();
} }
} }
/**
* 取元素集合中第一个元素的文本当elements为null或数量为0时返回空字符串
*
* @param elements 元素集合
* @return 第一个元素的文本
*/
public static String getElementText(Elements elements) {
return getElementText(elements, 0);
}
/**
* 取元素集合中指定索引元素的文本当elements为null时或index超出集合的大小时返回空字符串
*
* @param elements 元素集合
* @return 对应索引元素的文本
*/
public static String getElementText(Elements elements, int index) {
if (elements == null) {
return "";
} else {
return index >= elements.size() ? "" : elements.get(index).text();
}
}
} }

@ -27,22 +27,6 @@
<version>3.14.9</version> <version>3.14.9</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0-rc1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.4</version>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>

Loading…
Cancel
Save