Skip to content

Commit

Permalink
Date (#4216)
Browse files Browse the repository at this point in the history
* add version

* add stock_balance_sheet_by_report_delisted_em

* update date
  • Loading branch information
albertandking authored Oct 24, 2023
1 parent ed96659 commit d1502b5
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 98 deletions.
6 changes: 5 additions & 1 deletion akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2558,9 +2558,10 @@
1.11.42 fix: fix stock_gpzy_pledge_ratio_em interface
1.11.43 fix: fix get_futures_daily interface
1.11.44 add: add stock_cyq_em interface
1.11.45 add: add stock_balance_sheet_by_report_delisted_em interface
"""

__version__ = "1.11.44"
__version__ = "1.11.45"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -2924,6 +2925,9 @@
stock_cash_flow_sheet_by_report_em,
stock_cash_flow_sheet_by_quarterly_em,
stock_cash_flow_sheet_by_yearly_em,
stock_balance_sheet_by_report_delisted_em,
stock_profit_sheet_by_report_delisted_em,
stock_cash_flow_sheet_by_report_delisted_em,
)

"""
Expand Down
167 changes: 147 additions & 20 deletions akshare/stock_feature/stock_three_report_em.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2023/9/25 15:20
Date: 2023/10/24 15:20
Desc: 东方财富-股票-财务分析
"""
from functools import lru_cache
Expand All @@ -25,7 +25,7 @@ def _stock_balance_sheet_by_report_ctype_em(symbol: str = "SH600519") -> str:
url = f"https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/Index"
params = {"type": "web", "code": symbol.lower()}
r = requests.get(url, params=params)
soup = BeautifulSoup(r.text, "lxml")
soup = BeautifulSoup(r.text, features="lxml")
company_type = soup.find(attrs={"id": "hidctype"})["value"]
return company_type

Expand All @@ -52,7 +52,7 @@ def stock_balance_sheet_by_report_em(symbol: str = "SH600519") -> pd.DataFrame:
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_list = list()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/zcfzbAjaxNew"
Expand Down Expand Up @@ -100,7 +100,7 @@ def stock_balance_sheet_by_yearly_em(symbol: str = "SH600036") -> pd.DataFrame:
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/zcfzbAjaxNew"
Expand Down Expand Up @@ -140,7 +140,7 @@ def stock_profit_sheet_by_report_em(symbol: str = "SH600519") -> pd.DataFrame:
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/lrbAjaxNew"
Expand Down Expand Up @@ -180,7 +180,7 @@ def stock_profit_sheet_by_yearly_em(symbol: str = "SH600519") -> pd.DataFrame:
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/lrbAjaxNew"
Expand All @@ -199,7 +199,7 @@ def stock_profit_sheet_by_yearly_em(symbol: str = "SH600519") -> pd.DataFrame:


def stock_profit_sheet_by_quarterly_em(
symbol: str = "SH600519",
symbol: str = "SH600519",
) -> pd.DataFrame:
"""
东方财富-股票-财务分析-利润表-按单季度
Expand All @@ -222,7 +222,7 @@ def stock_profit_sheet_by_quarterly_em(
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/lrbAjaxNew"
Expand All @@ -241,7 +241,7 @@ def stock_profit_sheet_by_quarterly_em(


def stock_cash_flow_sheet_by_report_em(
symbol: str = "SH600519",
symbol: str = "SH600519",
) -> pd.DataFrame:
"""
东方财富-股票-财务分析-现金流量表-按报告期
Expand All @@ -264,7 +264,7 @@ def stock_cash_flow_sheet_by_report_em(
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/xjllbAjaxNew"
Expand All @@ -283,7 +283,7 @@ def stock_cash_flow_sheet_by_report_em(


def stock_cash_flow_sheet_by_yearly_em(
symbol: str = "SH600519",
symbol: str = "SH600519",
) -> pd.DataFrame:
"""
东方财富-股票-财务分析-现金流量表-按年度
Expand All @@ -306,7 +306,7 @@ def stock_cash_flow_sheet_by_yearly_em(
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/xjllbAjaxNew"
Expand All @@ -325,7 +325,7 @@ def stock_cash_flow_sheet_by_yearly_em(


def stock_cash_flow_sheet_by_quarterly_em(
symbol: str = "SH600519",
symbol: str = "SH600519",
) -> pd.DataFrame:
"""
东方财富-股票-财务分析-现金流量表-按单季度
Expand All @@ -348,7 +348,7 @@ def stock_cash_flow_sheet_by_quarterly_em(
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df["REPORT_DATE"] = temp_df["REPORT_DATE"].astype(str)
need_date = temp_df["REPORT_DATE"].tolist()
sep_list = [",".join(need_date[i : i + 5]) for i in range(0, len(need_date), 5)]
sep_list = [",".join(need_date[i: i + 5]) for i in range(0, len(need_date), 5)]
big_df = pd.DataFrame()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/xjllbAjaxNew"
Expand All @@ -366,14 +366,137 @@ def stock_cash_flow_sheet_by_quarterly_em(
return big_df


def __get_report_date_list_delisted_em(symbol: str = "SZ000013") -> list:
"""
东方财富-股票-财务分析-资产负债表-已退市股票-所有报告期
https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code=SZ000013
:param symbol: 已退市股票代码; 带市场标识
:type symbol: str
:return: 所有报告期
:rtype: list
"""
url = "https://datacenter.eastmoney.com/securities/api/data/get"
params = {
'type': 'RPT_F10_FINANCE_GINCOME',
'sty': 'SECURITY_CODE,REPORT_DATE,REPORT_TYPE,REPORT_DATE_NAME',
'filter': f'(SECURITY_CODE="{symbol[2:]}")',
'p': '1',
'ps': '200',
'sr': '-1',
'st': 'REPORT_DATE',
'source': 'HSF10',
'client': 'PC',
'v': '05767841728614413'
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json['result']["data"])
report_date_list = [item[0] for item in temp_df['REPORT_DATE'].str.split(" ")]
report_date_list = ["'" + item + "'" for item in report_date_list]
return report_date_list


def stock_balance_sheet_by_report_delisted_em(symbol: str = "SZ000013") -> pd.DataFrame:
"""
东方财富-股票-财务分析-资产负债表-已退市股票-按报告期
https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code=SZ000013#/cwfx/zcfzb
:param symbol: 已退市股票代码; 带市场标识
:type symbol: str
:return: 资产负债表-按报告期
:rtype: pandas.DataFrame
"""
report_date_list = __get_report_date_list_delisted_em(symbol)
url = "https://datacenter.eastmoney.com/securities/api/data/get"
params = {
'type': 'RPT_F10_FINANCE_GBALANCE',
'sty': 'F10_FINANCE_GBALANCE',
'filter': f"""(SECUCODE="{symbol[2:]}.{symbol[:2]}")(REPORT_DATE in ({','.join(report_date_list)}))""",
'p': '1',
'ps': '200',
'sr': '-1',
'st': 'REPORT_DATE',
'source': 'HSF10',
'client': 'PC',
'v': '05767841728614413'
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json['result']["data"])
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df.sort_values(by=['REPORT_DATE'], inplace=True, ignore_index=True)
return temp_df


def stock_profit_sheet_by_report_delisted_em(symbol: str = "SZ000013") -> pd.DataFrame:
"""
东方财富-股票-财务分析-利润表-已退市股票-按报告期
https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code=SZ000013#/cwfx/lrb
:param symbol: 已退市股票代码; 带市场标识
:type symbol: str
:return: 利润表-按报告期
:rtype: pandas.DataFrame
"""
report_date_list = __get_report_date_list_delisted_em(symbol)
url = "https://datacenter.eastmoney.com/securities/api/data/get"
params = {
'type': 'RPT_F10_FINANCE_GINCOME',
'sty': 'APP_F10_GINCOME',
'filter': f"""(SECUCODE="{symbol[2:]}.{symbol[:2]}")(REPORT_DATE in ({','.join(report_date_list)}))""",
'p': '1',
'ps': '200',
'sr': '-1',
'st': 'REPORT_DATE',
'source': 'HSF10',
'client': 'PC',
'v': '05767841728614413'
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json['result']["data"])
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df.sort_values(by=['REPORT_DATE'], inplace=True, ignore_index=True)
return temp_df


def stock_cash_flow_sheet_by_report_delisted_em(symbol: str = "SZ000013") -> pd.DataFrame:
"""
东方财富-股票-财务分析-现金流量表-已退市股票-按报告期
https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code=SZ000013#/cwfx/xjllb
:param symbol: 已退市股票代码; 带市场标识
:type symbol: str
:return: 现金流量表-按报告期
:rtype: pandas.DataFrame
"""
report_date_list = __get_report_date_list_delisted_em(symbol)
url = "https://datacenter.eastmoney.com/securities/api/data/get"
params = {
'type': 'RPT_F10_FINANCE_GCASHFLOW',
'sty': 'APP_F10_GCASHFLOW',
'filter': f"""(SECUCODE="{symbol[2:]}.{symbol[:2]}")(REPORT_DATE in ({','.join(report_date_list)}))""",
'p': '1',
'ps': '200',
'sr': '-1',
'st': 'REPORT_DATE',
'source': 'HSF10',
'client': 'PC',
'v': '05767841728614413'
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json['result']["data"])
temp_df["REPORT_DATE"] = pd.to_datetime(temp_df["REPORT_DATE"]).dt.date
temp_df.sort_values(by=['REPORT_DATE'], inplace=True, ignore_index=True)
return temp_df


if __name__ == "__main__":
stock_balance_sheet_by_report_em_df = stock_balance_sheet_by_report_em(
symbol="SH600519"
)
print(stock_balance_sheet_by_report_em_df)

stock_balance_sheet_by_yearly_em_df = stock_balance_sheet_by_yearly_em(
symbol="SH601318"
symbol="SH600519"
)
print(stock_balance_sheet_by_yearly_em_df)

Expand All @@ -382,11 +505,6 @@ def stock_cash_flow_sheet_by_quarterly_em(
)
print(stock_profit_sheet_by_report_em_df)

stock_profit_sheet_by_report_em_df = stock_profit_sheet_by_report_em(
symbol="SZ000001"
)
print(stock_profit_sheet_by_report_em_df)

stock_profit_sheet_by_yearly_em_df = stock_profit_sheet_by_yearly_em(
symbol="SH600519"
)
Expand All @@ -411,3 +529,12 @@ def stock_cash_flow_sheet_by_quarterly_em(
symbol="SH601398"
)
print(stock_cash_flow_sheet_by_quarterly_em_df)

stock_balance_sheet_by_report_delisted_em_df = stock_balance_sheet_by_report_delisted_em(symbol="SZ000013")
print(stock_balance_sheet_by_report_delisted_em_df)

stock_profit_sheet_by_report_delisted_em_df = stock_profit_sheet_by_report_delisted_em(symbol="SZ000013")
print(stock_profit_sheet_by_report_delisted_em_df)

stock_cash_flow_sheet_by_report_delisted_em_df = stock_cash_flow_sheet_by_report_delisted_em(symbol="SZ000013")
print(stock_cash_flow_sheet_by_report_delisted_em_df)
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@

## 更新说明详情

1.11.45 add: add stock_balance_sheet_by_report_delisted_em interface

1. 新增 stock_balance_sheet_by_report_delisted_em 接口
2. 新增 stock_profit_sheet_by_report_delisted_em 接口
3. 新增 stock_cash_flow_sheet_by_report_delisted_em 接口

1.11.44 add: add stock_cyq_em interface

1. 新增 stock_cyq_em 接口
Expand Down Expand Up @@ -3016,6 +3022,8 @@

## 版本更新说明

1.11.45 add: add stock_balance_sheet_by_report_delisted_em interface

1.11.44 add: add stock_cyq_em interface

1.11.43 fix: fix get_futures_daily interface
Expand Down
Loading

0 comments on commit d1502b5

Please sign in to comment.