Feat: 实现表格解析
This commit is contained in:
@@ -6,6 +6,11 @@ import org.jsoup.nodes.Element;
|
|||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SoupUtil {
|
public class SoupUtil {
|
||||||
|
|
||||||
@@ -13,9 +18,6 @@ public class SoupUtil {
|
|||||||
Document document = Jsoup.parse(content);
|
Document document = Jsoup.parse(content);
|
||||||
Elements elements = document.getElementsByClass("ui-table-hover");
|
Elements elements = document.getElementsByClass("ui-table-hover");
|
||||||
return elements;
|
return elements;
|
||||||
// Element firstTable = elements.first();
|
|
||||||
// Element secondTable = elements.size() >= 3 ? elements.get(2) : null;
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element getHoldings(Elements elements) {
|
public Element getHoldings(Elements elements) {
|
||||||
@@ -26,4 +28,28 @@ public class SoupUtil {
|
|||||||
return elements.size() >= 3 ? elements.get(2) : null;
|
return elements.size() >= 3 ? elements.get(2) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Map<String, String>> parseTable(Element table) {
|
||||||
|
Element tbody = table.select("tbody").getFirst();
|
||||||
|
// 获取表头
|
||||||
|
List<String> headers = new ArrayList<>();
|
||||||
|
Element headRow = tbody.select("tr").getFirst();
|
||||||
|
Elements headElements = headRow.select("th");
|
||||||
|
for (int i = 0; i < headElements.size() - 1; i++) {
|
||||||
|
headers.add(headElements.get(i).text());
|
||||||
|
}
|
||||||
|
// 获取表数据
|
||||||
|
List<Map<String, String>> data = new ArrayList<>();
|
||||||
|
Elements bodyElements = tbody.select("tr");
|
||||||
|
for (int i = 1; i < bodyElements.size(); i++) {
|
||||||
|
Elements tdElements = bodyElements.get(i).select("td");
|
||||||
|
Map<String, String> rowData = new HashMap<>();
|
||||||
|
for (int j = 0; j < tdElements.size() - 1; j++) {
|
||||||
|
rowData.put(headers.get(j), tdElements.get(j).text());
|
||||||
|
}
|
||||||
|
data.add(rowData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import org.jsoup.select.Elements;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class SoupUtilTest {
|
class SoupUtilTest {
|
||||||
@@ -39,4 +42,17 @@ class SoupUtilTest {
|
|||||||
System.out.println(worth);
|
System.out.println(worth);
|
||||||
Assert.notNull(worth);
|
Assert.notNull(worth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parseTable() {
|
||||||
|
Elements elements = soupUtil.parseContent(WebUtil.getContentByUrl(FundUtil.getUrlByFundId("001068")));
|
||||||
|
Assert.notNull(elements);
|
||||||
|
|
||||||
|
Element holdings = soupUtil.getHoldings(elements);
|
||||||
|
Assert.notNull(holdings);
|
||||||
|
|
||||||
|
List<Map<String, String>> maps = soupUtil.parseTable(holdings);
|
||||||
|
Assert.notNull(maps);
|
||||||
|
System.out.println(maps);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user