Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
731fdb4bde | ||
|
|
733ea32b5d |
@@ -1,7 +1,9 @@
|
||||
package cn.hezhaohui.fund;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FundUtil {
|
||||
final private static String PREFIX = "https://fund.eastmoney.com/";
|
||||
final private static String SUFFIX = ".html";
|
||||
|
||||
// example: https://fund.eastmoney.com/001068.html
|
||||
public static String getUrlByFundId(String id) {
|
||||
return PREFIX + id + SUFFIX;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SoupUtil {
|
||||
|
||||
public Elements parseContent(String content) {
|
||||
Document document = Jsoup.parse(content);
|
||||
Elements elements = document.getElementsByClass("ui-table-hover");
|
||||
return elements;
|
||||
// Element firstTable = elements.first();
|
||||
// Element secondTable = elements.size() >= 3 ? elements.get(2) : null;
|
||||
//
|
||||
}
|
||||
|
||||
public Element getHoldings(Elements elements) {
|
||||
return !elements.isEmpty() ? elements.getFirst() : null;
|
||||
}
|
||||
|
||||
public Element getWorth(Elements elements) {
|
||||
return elements.size() >= 3 ? elements.get(2) : null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FundUtilTest {
|
||||
|
||||
@Test
|
||||
void getUrlByFundId() {
|
||||
String url = FundUtil.getUrlByFundId("001068");
|
||||
System.out.println(url);
|
||||
assertNotNull(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
class SoupUtilTest {
|
||||
@Resource
|
||||
private SoupUtil soupUtil;
|
||||
|
||||
@Test
|
||||
void parseContent() {
|
||||
Elements elements = soupUtil.parseContent(WebUtil.getContentByUrl(FundUtil.getUrlByFundId("001068")));
|
||||
System.out.println(elements);
|
||||
Assert.notNull(elements);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHoldings() {
|
||||
Elements elements = soupUtil.parseContent(WebUtil.getContentByUrl(FundUtil.getUrlByFundId("001068")));
|
||||
Assert.notNull(elements);
|
||||
|
||||
Element holdings = soupUtil.getHoldings(elements);
|
||||
System.out.println(holdings);
|
||||
Assert.notNull(holdings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWorth() {
|
||||
Elements elements = soupUtil.parseContent(WebUtil.getContentByUrl(FundUtil.getUrlByFundId("001068")));
|
||||
Assert.notNull(elements);
|
||||
|
||||
Element worth = soupUtil.getWorth(elements);
|
||||
System.out.println(worth);
|
||||
Assert.notNull(worth);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user