Compare commits
4
Commits
731fdb4bde
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c470ad16f | ||
|
|
7466685a49 | ||
|
|
e2d5c39805 | ||
|
|
6f5884af2b |
@@ -1,3 +1,64 @@
|
|||||||
# fund-crawler
|
# fund-crawler
|
||||||
|
|
||||||
🐛 基于 Jsoup 爬取东方财富网基金内容
|
🐛 基于 Jsoup 爬取东方财富网基金内容
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## 项目介绍
|
||||||
|
|
||||||
|
这是一个基于 Java 的基金数据爬虫项目,使用 Jsoup 库解析东方财富网的基金数据。该项目旨在定期获取基金的实时信息,包括基金净值、涨跌幅等关键数据。
|
||||||
|
|
||||||
|
## 技术栈
|
||||||
|
|
||||||
|
- **Java 8+** - 项目主要编程语言
|
||||||
|
- **Spring Boot** - 应用框架
|
||||||
|
- **Jsoup** - HTML 解析库
|
||||||
|
- **Hutool** - Java 工具库
|
||||||
|
- **Maven** - 项目构建工具
|
||||||
|
- **JUnit** - 单元测试框架
|
||||||
|
|
||||||
|
## 项目结构
|
||||||
|
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
├── main/
|
||||||
|
│ ├── java/
|
||||||
|
│ │ └── cn/hezhaohui/fund/
|
||||||
|
│ │ ├── Application.java # Spring Boot 启动类
|
||||||
|
│ │ ├── util/
|
||||||
|
│ │ │ ├── FundUtil.java # 核心基金数据获取工具类
|
||||||
|
│ │ │ ├── SoupUtil.java # HTML 解析工具类
|
||||||
|
│ │ │ └── WebUtil.java # 网页内容获取工具类
|
||||||
|
│ └── resources/
|
||||||
|
├── test/
|
||||||
|
│ └── java/
|
||||||
|
│ └── cn/hezhaohui/fund/
|
||||||
|
│ ├── util/
|
||||||
|
│ │ ├── FundUtilTest.java # FundUtil 单元测试
|
||||||
|
│ │ ├── SoupUtilTest.java # SoupUtil 单元测试
|
||||||
|
│ │ └── WebUtilTest.java # WebUtil 单元测试
|
||||||
|
```
|
||||||
|
|
||||||
|
## 功能特性
|
||||||
|
|
||||||
|
- 爬取东方财富网基金数据
|
||||||
|
- 解析基金净值、涨跌幅等信息
|
||||||
|
- 提供基金数据的结构化输出
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 开发和测试
|
||||||
|
|
||||||
|
项目包含完整的单元测试,确保代码质量:
|
||||||
|
|
||||||
|
- `FundUtilTest` - 测试基金数据获取功能
|
||||||
|
- `SoupUtilTest` - 测试 HTML 解析功能
|
||||||
|
- `WebUtilTest` - 测试网页内容获取功能
|
||||||
|
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
- 本项目仅供学习交流使用
|
||||||
|
- 请遵守目标网站的爬虫协议
|
||||||
|
- 不要频繁请求,避免对服务器造成压力
|
||||||
|
- 数据仅供参考,不构成投资建议
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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