Feat: 实现根据基金 id 返回持仓以及净值信息
This commit is contained in:
@@ -1,9 +1,18 @@
|
|||||||
package cn.hezhaohui.fund.util;
|
package cn.hezhaohui.fund.util;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FundUtil {
|
public class FundUtil {
|
||||||
|
@Resource
|
||||||
|
private SoupUtil soupUtil;
|
||||||
|
|
||||||
final private static String PREFIX = "https://fund.eastmoney.com/";
|
final private static String PREFIX = "https://fund.eastmoney.com/";
|
||||||
final private static String SUFFIX = ".html";
|
final private static String SUFFIX = ".html";
|
||||||
|
|
||||||
@@ -11,4 +20,20 @@ public class FundUtil {
|
|||||||
public static String getUrlByFundId(String id) {
|
public static String getUrlByFundId(String id) {
|
||||||
return PREFIX + id + SUFFIX;
|
return PREFIX + id + SUFFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Map<String, String>> getWorthByFundId(String id) {
|
||||||
|
String url = getUrlByFundId(id);
|
||||||
|
String content = WebUtil.getContentByUrl(url);
|
||||||
|
Elements parsedContent = soupUtil.parseContent(content);
|
||||||
|
Element worth = soupUtil.getWorth(parsedContent);
|
||||||
|
return soupUtil.parseTable(worth);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Map<String, String>> getHoldingsByFundId(String id) {
|
||||||
|
String url = getUrlByFundId(id);
|
||||||
|
String content = WebUtil.getContentByUrl(url);
|
||||||
|
Elements parsedContent = soupUtil.parseContent(content);
|
||||||
|
Element holdings = soupUtil.getHoldings(parsedContent);
|
||||||
|
return soupUtil.parseTable(holdings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,19 @@
|
|||||||
package cn.hezhaohui.fund.util;
|
package cn.hezhaohui.fund.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
class FundUtilTest {
|
class FundUtilTest {
|
||||||
|
@Resource
|
||||||
|
private FundUtil fundUtil;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUrlByFundId() {
|
void getUrlByFundId() {
|
||||||
@@ -12,4 +21,18 @@ class FundUtilTest {
|
|||||||
System.out.println(url);
|
System.out.println(url);
|
||||||
assertNotNull(url);
|
assertNotNull(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getWorthByFundId() {
|
||||||
|
List<Map<String, String>> worth = fundUtil.getWorthByFundId("001068");
|
||||||
|
Assert.notNull(worth);
|
||||||
|
System.out.println(worth);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getHoldingsByFundId() {
|
||||||
|
List<Map<String, String>> holdings = fundUtil.getHoldingsByFundId("001068");
|
||||||
|
Assert.notNull(holdings);
|
||||||
|
System.out.println(holdings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user