Skip to content

Commit

Permalink
Merge pull request akfamily#3423 from akfamily/develop
Browse files Browse the repository at this point in the history
feat(stock_zygc.py): add stock_zygc_em interface
  • Loading branch information
albertandking authored Jan 30, 2023
2 parents eeddc51 + 1096e4c commit b222471
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 76 deletions.
5 changes: 3 additions & 2 deletions akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2287,9 +2287,10 @@
1.8.68 fix: fix stock_individual_fund_flow interface
1.8.69 fix: fix stock_individual_fund_flow_rank interface
1.8.70 add: add stock_market_pe_lg interface
1.8.71 add: add stock_zygc_em interface
"""

__version__ = "1.8.70"
__version__ = "1.8.71"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -2465,7 +2466,7 @@
"""
主营构成
"""
from akshare.stock_fundamental.stock_zygc_ym import stock_zygc_ym
from akshare.stock_fundamental.stock_zygc import stock_zygc_ym, stock_zygc_em

"""
人民币汇率中间价
Expand Down
2 changes: 1 addition & 1 deletion akshare/stock_feature/stock_a_indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def stock_a_lg_indicator(symbol: str = "000001") -> pd.DataFrame:
"""
市盈率, 市净率, 股息率数据接口
https://legulegu.com/stocklist
:param symbol: 通过 ak.stock_a_lg_indicator(stock="all") 来获取所有股票的代码
:param symbol: 通过 ak.stock_a_lg_indicator(symbol="all") 来获取所有股票的代码
:type symbol: str
:return: 市盈率, 市净率, 股息率查询
:rtype: pandas.DataFrame
Expand Down
131 changes: 131 additions & 0 deletions akshare/stock_fundamental/stock_zygc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2023/1/30 11:30
Desc: 主营构成
https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/Index?type=web&code=SH688041#
http://f10.emoney.cn/f10/zbyz/1000001
"""
import pandas as pd
import requests
from bs4 import BeautifulSoup


def stock_zygc_ym(symbol: str = "000001") -> pd.DataFrame:
"""
益盟-F10-主营构成
http://f10.emoney.cn/f10/zbyz/1000001
:param symbol: 股票代码
:type symbol: str
:return: 主营构成
:rtype: pandas.DataFrame
"""
url = f"http://f10.emoney.cn/f10/zygc/{symbol}"
r = requests.get(url)
soup = BeautifulSoup(r.text, "lxml")
year_list = [
item.text.strip()
for item in soup.find(attrs={"class": "swlab_t"}).find_all("li")
]

big_df = pd.DataFrame()
for i, item in enumerate(year_list, 2):
temp_df = pd.read_html(r.text, header=0)[i]
temp_df.columns = [
"分类方向",
"分类",
"营业收入",
"营业收入-同比增长",
"营业收入-占主营收入比",
"营业成本",
"营业成本-同比增长",
"营业成本-占主营成本比",
"毛利率",
"毛利率-同比增长",
]
temp_df["报告期"] = item
big_df = pd.concat([big_df, temp_df], ignore_index=True)

big_df = big_df[
[
"报告期",
"分类方向",
"分类",
"营业收入",
"营业收入-同比增长",
"营业收入-占主营收入比",
"营业成本",
"营业成本-同比增长",
"营业成本-占主营成本比",
"毛利率",
"毛利率-同比增长",
]
]
return big_df


def stock_zygc_em(symbol: str = "SH688041") -> pd.DataFrame:
"""
东方财富网-个股-主营构成
https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/Index?type=web&code=SH688041#
:param symbol: 带市场标识的股票代码
:type symbol: str
:return: 主营构成
:rtype: pandas.DataFrame
"""
url = "https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/PageAjax"
params = {"code": symbol}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["zygcfx"])
temp_df.rename(
columns={
"SECUCODE": "-",
"SECURITY_CODE": "股票代码",
"REPORT_DATE": "报告日期",
"MAINOP_TYPE": "分类类型",
"ITEM_NAME": "主营构成",
"MAIN_BUSINESS_INCOME": "主营收入",
"MBI_RATIO": "收入比例",
"MAIN_BUSINESS_COST": "主营成本",
"MBC_RATIO": "成本比例",
"MAIN_BUSINESS_RPOFIT": "主营利润",
"MBR_RATIO": "利润比例",
"GROSS_RPOFIT_RATIO": "毛利率",
"RANK": "-",
},
inplace=True,
)
temp_df = temp_df[
[
"股票代码",
"报告日期",
"分类类型",
"主营构成",
"主营收入",
"收入比例",
"主营成本",
"成本比例",
"主营利润",
"利润比例",
"毛利率",
]
]
temp_df["报告日期"] = pd.to_datetime(temp_df["报告日期"]).dt.date
temp_df["分类类型"] = temp_df["分类类型"].map({"2": "按产品分类", "3": "按地区分类"})
temp_df["主营收入"] = pd.to_numeric(temp_df["主营收入"], errors="coerce")
temp_df["收入比例"] = pd.to_numeric(temp_df["收入比例"], errors="coerce")
temp_df["主营成本"] = pd.to_numeric(temp_df["主营成本"], errors="coerce")
temp_df["成本比例"] = pd.to_numeric(temp_df["成本比例"], errors="coerce")
temp_df["主营利润"] = pd.to_numeric(temp_df["主营利润"], errors="coerce")
temp_df["利润比例"] = pd.to_numeric(temp_df["利润比例"], errors="coerce")
temp_df["毛利率"] = pd.to_numeric(temp_df["毛利率"], errors="coerce")
return temp_df


if __name__ == "__main__":
stock_zygc_ym_df = stock_zygc_ym(symbol="000001")
print(stock_zygc_ym_df)

stock_zygc_em_df = stock_zygc_em(symbol="SH688041")
print(stock_zygc_em_df)
68 changes: 0 additions & 68 deletions akshare/stock_fundamental/stock_zygc_ym.py

This file was deleted.

6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@

## 更新说明

1.8.71 add: add stock_zygc_em interface

1. 新增 stock_zygc_em 接口

1.8.70 add: add stock_market_pe_lg interface

1. 新增 stock_market_pe_lg 接口
Expand Down Expand Up @@ -1830,6 +1834,8 @@

## 版本更新说明

1.8.71 add: add stock_zygc_em interface

1.8.70 add: add stock_market_pe_lg interface

1.8.69 fix: fix stock_individual_fund_flow_rank interface
Expand Down
104 changes: 100 additions & 4 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -4357,7 +4357,103 @@ print(stock_jgdy_detail_em_df)
623 624 300718 长盛轴承 ... 浙江长盛滑动轴承股份有限公司 2021-09-16 2021-09-16
```

### 主营构成
### 主营构成-东财

接口: stock_zygc_em

目标地址: https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/Index?type=web&code=SH688041#

描述: 东方财富网-个股-主营构成

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

输入参数

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

输出参数

| 名称 | 类型 | 描述 |
|------|---------|---------|
| 股票代码 | object | - |
| 报告日期 | object | - |
| 分类类型 | object | - |
| 主营构成 | int64 | - |
| 主营收入 | float64 | 注意单位: 元 |
| 收入比例 | float64 | - |
| 主营成本 | float64 | 注意单位: 元 |
| 成本比例 | float64 | - |
| 主营利润 | float64 | 注意单位: 元 |
| 利润比例 | float64 | - |
| 毛利率 | float64 | - |

接口示例

```python
import akshare as ak

stock_zygc_em_df = ak.stock_zygc_em(symbol="SH688041")
print(stock_zygc_em_df)
```

数据示例

```
股票代码 报告日期 分类类型 ... 主营利润 利润比例 毛利率
0 688041 2021-12-31 按产品分类 ... 984716100.0 0.761747 0.655598
1 688041 2021-12-31 按产品分类 ... 102370900.0 0.079191 0.271176
2 688041 2021-12-31 按产品分类 ... 83252700.0 0.064402 0.348428
3 688041 2021-12-31 按产品分类 ... 122326800.0 0.094628 0.637574
4 688041 2021-12-31 按产品分类 ... 40700.0 0.000031 0.431601
5 688041 2021-12-31 按产品分类 ... 880201700.0 0.680898 0.720836
6 688041 2021-12-31 按产品分类 ... 106466300.0 0.082359 0.289718
7 688041 2021-12-31 按产品分类 ... 104514300.0 0.080849 0.372031
8 688041 2021-12-31 按产品分类 ... 83252700.0 0.064402 0.348428
9 688041 2021-12-31 按产品分类 ... 124702500.0 0.096466 0.652495
10 688041 2021-12-31 按产品分类 ... -4095400.0 -0.003168 -0.408535
11 688041 2021-12-31 按产品分类 ... -2375600.0 -0.001838 -3.182317
12 688041 2021-12-31 按地区分类 ... NaN NaN NaN
13 688041 2021-12-31 按地区分类 ... NaN NaN NaN
14 688041 2021-12-31 按地区分类 ... NaN NaN NaN
15 688041 2021-12-31 按地区分类 ... NaN NaN NaN
16 688041 2021-12-31 按地区分类 ... NaN NaN NaN
17 688041 2021-12-31 按地区分类 ... NaN NaN NaN
18 688041 2021-06-30 按产品分类 ... 298423500.0 0.743139 0.767885
19 688041 2021-06-30 按产品分类 ... 57928200.0 0.144254 0.475693
20 688041 2021-06-30 按产品分类 ... 45219800.0 0.112607 0.741744
21 688041 2021-06-30 按地区分类 ... NaN NaN NaN
22 688041 2021-06-30 按地区分类 ... NaN NaN NaN
23 688041 2021-06-30 按地区分类 ... NaN NaN NaN
24 688041 2021-06-30 按地区分类 ... NaN NaN NaN
25 688041 2021-06-30 按地区分类 ... NaN NaN NaN
26 688041 2020-12-31 按产品分类 ... 546891800.0 0.790741 0.695846
27 688041 2020-12-31 按产品分类 ... 101902800.0 0.147339 0.788001
28 688041 2020-12-31 按产品分类 ... 42783500.0 0.061860 0.401261
29 688041 2020-12-31 按产品分类 ... 41600.0 0.000060 0.441145
30 688041 2020-12-31 按地区分类 ... NaN NaN NaN
31 688041 2020-12-31 按地区分类 ... NaN NaN NaN
32 688041 2020-12-31 按地区分类 ... NaN NaN NaN
33 688041 2020-12-31 按地区分类 ... NaN NaN NaN
34 688041 2020-12-31 按地区分类 ... NaN NaN NaN
35 688041 2020-12-31 按地区分类 ... NaN NaN NaN
36 688041 2019-12-31 按产品分类 ... 233396100.0 0.877175 0.718299
37 688041 2019-12-31 按产品分类 ... 28995900.0 0.108976 0.593399
38 688041 2019-12-31 按产品分类 ... 3685000.0 0.013849 0.685926
39 688041 2019-12-31 按地区分类 ... NaN NaN NaN
40 688041 2019-12-31 按地区分类 ... NaN NaN NaN
41 688041 2019-12-31 按地区分类 ... NaN NaN NaN
42 688041 2019-12-31 按地区分类 ... NaN NaN NaN
43 688041 2019-12-31 按地区分类 ... NaN NaN NaN
44 688041 2018-12-31 按产品分类 ... 35389800.0 0.890480 0.841592
45 688041 2018-12-31 按产品分类 ... 4352600.0 0.109520 0.792982
46 688041 2018-12-31 按产品分类 ... NaN NaN NaN
47 688041 2018-12-31 按地区分类 ... NaN NaN NaN
48 688041 2018-12-31 按地区分类 ... NaN NaN NaN
```

### 主营构成-益盟

接口: stock_zygc_ym

Expand Down Expand Up @@ -13780,9 +13876,9 @@ print(stock_a_pb_df)

输入参数

| 名称 | 类型 | 描述 |
|--------|-----|---------------------------------------------------------------------|
| symbol | str | symbol="000001"; 参见 **ak.stock_a_lg_indicator(stock="all")** 获取股票代码 |
| 名称 | 类型 | 描述 |
|--------|-----|----------------------------------------------------------------------|
| symbol | str | symbol="000001"; 参见 **ak.stock_a_lg_indicator(symbol="all")** 获取股票代码 |

输出参数

Expand Down
3 changes: 2 additions & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,8 @@
# 人民币汇率中间价
"currency_boc_safe" # 人民币汇率中间价
# 主营构成
"stock_zygc_ym" # 主营构成
"stock_zygc_ym" # 主营构成-益盟
"stock_zygc_em" # 主营构成-东财
# 管理层讨论与分析
"stock_mda_ym" # 管理层讨论与分析
# 行业分类数据
Expand Down

0 comments on commit b222471

Please sign in to comment.