Compare commits
6
Commits
644af33c20
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c470ad16f | ||
|
|
7466685a49 | ||
|
|
e2d5c39805 | ||
|
|
6f5884af2b | ||
|
|
731fdb4bde | ||
|
|
733ea32b5d |
@@ -1,3 +1,64 @@
|
||||
# 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,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,39 @@
|
||||
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 java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class FundUtil {
|
||||
@Resource
|
||||
private SoupUtil soupUtil;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class SoupUtil {
|
||||
|
||||
public Elements parseContent(String content) {
|
||||
Document document = Jsoup.parse(content);
|
||||
Elements elements = document.getElementsByClass("ui-table-hover");
|
||||
return elements;
|
||||
}
|
||||
|
||||
public Element getHoldings(Elements elements) {
|
||||
return !elements.isEmpty() ? elements.getFirst() : null;
|
||||
}
|
||||
|
||||
public Element getWorth(Elements elements) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import jakarta.annotation.Resource;
|
||||
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.*;
|
||||
|
||||
@SpringBootTest
|
||||
class FundUtilTest {
|
||||
@Resource
|
||||
private FundUtil fundUtil;
|
||||
|
||||
@Test
|
||||
void getUrlByFundId() {
|
||||
String url = FundUtil.getUrlByFundId("001068");
|
||||
System.out.println(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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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