Skip to content

Commit

Permalink
fix: fix fund_etf_dividend_sina
Browse files Browse the repository at this point in the history
  • Loading branch information
albertandking committed Jan 5, 2025
1 parent 36020dd commit 530d2f9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
48 changes: 46 additions & 2 deletions akshare/fund/fund_etf_sina.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/11/22 14:00
Date: 2025/1/5 19:00
Desc: 新浪财经-基金行情
https://vip.stock.finance.sina.com.cn/fund_center/index.html#jjhqetf
"""
Expand Down Expand Up @@ -132,15 +132,56 @@ def fund_etf_hist_sina(symbol: str = "sh510050") -> pd.DataFrame:
temp_df = pd.DataFrame(dict_list)
if temp_df.empty: # 处理获取数据为空的问题
return pd.DataFrame()
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.tz_localize(
None
)
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")

# 转换日期列为日期类型
temp_df["date"] = temp_df["date"].dt.date
temp_df = temp_df.sort_values(by="date", ascending=True)
return temp_df


def fund_etf_dividend_sina(symbol: str = "sh510050") -> pd.DataFrame:
"""
新浪财经-基金-ETF 基金-累计分红
https://finance.sina.com.cn/fund/quotes/510050/bc.shtml
:param symbol: 基金名称, 可以通过 ak.fund_etf_category_sina() 函数获取
:type symbol: str
:return: 累计分红
:rtype: pandas.DataFrame
"""
# 构建复权数据URL
factor_url = f"https://finance.sina.com.cn/realstock/company/{symbol}/hfq.js"
r = requests.get(factor_url)
text = r.text
if text.startswith("var"):
json_str = text.split("=")[1].strip().rsplit("}", maxsplit=1)[0].strip()
data = eval(json_str + "}") # 这里使用eval而不是json.loads因为数据格式特殊

if isinstance(data, dict) and "data" in data:
df = pd.DataFrame(data["data"])
# 重命名列
df.columns = ["date", "f", "s", "u"] if len(df.columns) == 4 else df.columns
# 移除1900-01-01的数据
df = df[df["date"] != "1900-01-01"]
# 转换日期
df["date"] = pd.to_datetime(df["date"])
# 转换数值类型
df[["f", "s", "u"]] = df[["f", "s", "u"]].astype(float)
# 按日期排序
df = df.sort_values("date", ascending=True, ignore_index=True)
temp_df = df[["date", "u"]].copy()
temp_df.columns = ["日期", "累计分红"]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
return temp_df


if __name__ == "__main__":
fund_etf_category_sina_df = fund_etf_category_sina(symbol="封闭式基金")
print(fund_etf_category_sina_df)
Expand All @@ -156,3 +197,6 @@ def fund_etf_hist_sina(symbol: str = "sh510050") -> pd.DataFrame:

fund_etf_hist_sina_df = fund_etf_hist_sina(symbol="sh510300")
print(fund_etf_hist_sina_df)

fund_etf_dividend_sina_df = fund_etf_dividend_sina(symbol="sh510050")
print(fund_etf_dividend_sina_df)
57 changes: 56 additions & 1 deletion docs/data/fund/fund_public.md
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,7 @@ print(fund_hk_fund_hist_em_df)
数据示例-分红送配详情

```
年份 权益登记日 除息日 分红发放日 分红金额 单位
年份权益登记日 除息日 分红发放日 分红金额 单位
0 2020 2020-12-31 0.0522 元
1 2020 2020-11-30 2020-11-30 2020-12-14 0.0552 元
2 2020 2020-10-30 2020-10-30 2020-11-13 0.0573 元
Expand All @@ -2011,6 +2011,61 @@ print(fund_hk_fund_hist_em_df)

### 分红送配

#### 基金累计分红

接口: fund_etf_dividend_sina

目标地址: https://finance.sina.com.cn/fund/quotes/510050/bc.shtml

描述: 新浪财经-基金-ETF 基金-累计分红

限量: 单次返回所有历史数据

输入参数

| 名称 | 类型 | 描述 |
|--------|-----|-------------------|
| symbol | str | symbol="sh510050" |

输出参数

| 名称 | 类型 | 描述 |
|------|---------|-------|
| 日期 | object | 除权除息日 |
| 累计分红 | float64 | - |

接口示例

```python
import akshare as ak

fund_etf_dividend_sina_df = fund_etf_dividend_sina(symbol="sh510050")
print(fund_etf_dividend_sina_df)
```

数据示例

```
日期 累计分红
0 2006-05-19 0.024
1 2006-11-16 0.061
2 2008-11-19 0.121
3 2010-11-16 0.147
4 2012-05-16 0.158
5 2012-11-13 0.195
6 2013-11-15 0.248
7 2014-11-17 0.291
8 2016-11-29 0.344
9 2017-11-28 0.398
10 2018-12-03 0.447
11 2019-12-02 0.494
12 2020-11-30 0.545
13 2021-11-29 0.586
14 2022-12-01 0.623
15 2023-11-27 0.662
16 2024-12-02 0.717
```

#### 基金分红

接口: fund_fh_em
Expand Down
1 change: 1 addition & 0 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@
# 基金行情
"fund_etf_category_sina" # 基金实时行情-新浪
"fund_etf_hist_sina" # 基金行情-新浪
"fund_etf_dividend_sina" # 新浪财经-基金-ETF 基金-累计分红
"fund_etf_hist_em" # 基金历史行情-东财
"fund_etf_hist_min_em" # 基金分时行情-东财
"fund_etf_spot_em" # 基金实时行情-东财
Expand Down

0 comments on commit 530d2f9

Please sign in to comment.