Compare commits
8
Commits
81658f0b3a
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c470ad16f | ||
|
|
7466685a49 | ||
|
|
e2d5c39805 | ||
|
|
6f5884af2b | ||
|
|
731fdb4bde | ||
|
|
733ea32b5d | ||
|
|
644af33c20 | ||
|
|
a064e422eb |
@@ -0,0 +1,2 @@
|
||||
*.idea/
|
||||
target
|
||||
@@ -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` - 测试网页内容获取功能
|
||||
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 本项目仅供学习交流使用
|
||||
- 请遵守目标网站的爬虫协议
|
||||
- 不要频繁请求,避免对服务器造成压力
|
||||
- 数据仅供参考,不构成投资建议
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.hezhaohui</groupId>
|
||||
<artifactId>fund-crawler</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.2</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>1.17.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-http -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
<version>5.8.31</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
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,12 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class WebUtil {
|
||||
public static String getContentByUrl(String url) {
|
||||
String string = HttpUtil.get(url);
|
||||
return string;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.hezhaohui.fund.util;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class WebUtilTest {
|
||||
|
||||
@Test
|
||||
void getContentByUrl() {
|
||||
String content = WebUtil.getContentByUrl("https://fund.eastmoney.com/001068.html");
|
||||
System.out.println(content);
|
||||
Assert.notNull(content);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user