Skip to content

Commit

Permalink
feat(stock_szse_sector_summary): add stock_szse_sector_summary interface
Browse files Browse the repository at this point in the history
add stock_szse_sector_summary interface
  • Loading branch information
ak-quant committed May 18, 2022
1 parent 3847cca commit c8ab581
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
4 changes: 3 additions & 1 deletion akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,9 +1999,10 @@
1.5.77 fix: fix remove numpy dependency
1.5.78 fix: fix stock_szse_summary interface
1.5.79 add: add stock_szse_area_summary interface
1.5.80 add: add stock_szse_sector_summary interface
"""

__version__ = "1.5.79"
__version__ = "1.5.80"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -3048,6 +3049,7 @@
stock_szse_summary,
stock_sse_deal_daily,
stock_szse_area_summary,
stock_szse_sector_summary,
)

"""
Expand Down
65 changes: 65 additions & 0 deletions akshare/stock/stock_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pandas as pd
import requests
from bs4 import BeautifulSoup


def stock_szse_summary(date: str = "20200619") -> pd.DataFrame:
Expand Down Expand Up @@ -79,6 +80,67 @@ def stock_szse_area_summary(date: str = "202203") -> pd.DataFrame:
return temp_df


def stock_szse_sector_summary(symbol: str = "当月", date: str = "202203") -> pd.DataFrame:
"""
深圳证券交易所-统计资料-股票行业成交
http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html
:param symbol: choice of {"当月", "当年"}
:type symbol: str
:param date: 交易年月
:type date: str
:return: 股票行业成交
:rtype: pandas.DataFrame
"""
url = "https://www.szse.cn/market/periodical/month/index.html"
r = requests.get(url)
r.encoding = "utf8"
soup = BeautifulSoup(r.text, "lxml")
tags_list = soup.find_all("div", attrs={"class": "g-container"})[4].find_all("script")
tags_dict = [eval(item.string[item.string.find("{"): item.string.find("}")+1].replace("\n", "").replace(" ", "").replace("value", "'value'").replace("text", "'text'")) for item in tags_list]
date_url_dict = dict(zip([item['text'] for item in tags_dict], [item['value'][2:] for item in tags_dict]))
date_format = '-'.join([date[:4], date[4:]])
url = f"http://www.szse.cn/market/periodical/month/{date_url_dict[date_format]}"
r = requests.get(url)
r.encoding = "utf8"
soup = BeautifulSoup(r.text, "lxml")
url = soup.find("a", text="股票行业成交数据")['href']
if symbol == "当月":
temp_df = pd.read_html(url, encoding="gbk")[0]
temp_df.columns = [
"项目名称",
"项目名称-英文",
"交易天数",
"成交金额-人民币元",
"成交金额-占总计",
"成交股数-股数",
"成交股数-占总计",
"成交笔数-笔",
"成交笔数-占总计",
]
else:
temp_df = pd.read_html(url, encoding="gbk")[1]
temp_df.columns = [
"项目名称",
"项目名称-英文",
"交易天数",
"成交金额-人民币元",
"成交金额-占总计",
"成交股数-股数",
"成交股数-占总计",
"成交笔数-笔",
"成交笔数-占总计",
]

temp_df['交易天数'] = pd.to_numeric(temp_df['交易天数'])
temp_df['成交金额-人民币元'] = pd.to_numeric(temp_df['成交金额-人民币元'])
temp_df['成交金额-占总计'] = pd.to_numeric(temp_df['成交金额-占总计'])
temp_df['成交股数-股数'] = pd.to_numeric(temp_df['成交股数-股数'])
temp_df['成交股数-占总计'] = pd.to_numeric(temp_df['成交股数-占总计'])
temp_df['成交笔数-笔'] = pd.to_numeric(temp_df['成交笔数-笔'])
temp_df['成交笔数-占总计'] = pd.to_numeric(temp_df['成交笔数-占总计'])
return temp_df


def stock_sse_summary() -> pd.DataFrame:
"""
上海证券交易所-总貌
Expand Down Expand Up @@ -367,6 +429,9 @@ def stock_sse_deal_daily(date: str = "20220331") -> pd.DataFrame:
stock_szse_area_summary_df = stock_szse_area_summary(date="202203")
print(stock_szse_area_summary_df)

stock_szse_sector_summary_df = stock_szse_sector_summary(symbol="当年", date="202204")
print(stock_szse_sector_summary_df)

stock_sse_summary_df = stock_sse_summary()
print(stock_sse_summary_df)

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

## 更新说明

1.5.80 add: add stock_szse_sector_summary interface

1. 新增 stock_szse_sector_summary 接口,获取深证证券交易所-总貌-股票行业成交数据

1.5.79 add: add stock_szse_area_summary interface

1. 新增 stock_szse_area_summary 接口,获取深证证券交易所-总貌-地区交易排序数据
Expand Down Expand Up @@ -593,6 +597,8 @@

## 版本更新说明

1.5.80 add: add stock_szse_sector_summary interface

1.5.79 add: add stock_szse_area_summary interface

1.5.78 fix: fix stock_szse_summary interface
Expand Down
66 changes: 66 additions & 0 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,72 @@ print(stock_szse_area_summary_df)
33 34 青海 1.783947e+10 0.046 1.518497e+10 1.181823e+08 2.536324e+09
```

###### 股票行业成交

接口: stock_szse_sector_summary

目标地址: http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html

描述: 深圳证券交易所-统计资料-股票行业成交

限量: 单次返回指定 date 和 symbol 的统计资料-股票行业成交数据

输入参数

| 名称 | 类型 | 描述 |
|--------|-----|-------------------------------------|
| symbol | str | symbol="当月"; choice of {"当月", "当年"} |
| date | str | date="202203"; 年月 |

输出参数

| 名称 | 类型 | 描述 |
|-----------|---------|---------|
| 项目名称 | object | - |
| 项目名称-英文 | object | - |
| 交易天数 | int64 | - |
| 成交金额-人民币元 | int64 | |
| 成交金额-占总计 | float64 | 注意单位: % |
| 成交股数-股数 | int64 | - |
| 成交股数-占总计 | float64 | 注意单位: % |
| 成交笔数-笔 | int64 | - |
| 成交笔数-占总计 | float64 | 注意单位: % |

接口示例

```python
import akshare as ak

stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol="当年", date="202204")
print(stock_szse_sector_summary_df)
```

数据示例

```
项目名称 项目名称-英文 交易天数 ... 成交股数-占总计 成交笔数-笔 成交笔数-占总计
0 合计 Total 77 ... 100.00 3816547870 100.00
1 农林牧渔 Agriculture 77 ... 1.51 60126514 1.58
2 采矿业 Mining 77 ... 2.10 72850554 1.91
3 制造业 Manufacturing 77 ... 55.08 2404587520 63.00
4 水电煤气 Utilities 77 ... 2.27 68543100 1.80
5 建筑业 Construction 77 ... 3.13 87826462 2.30
6 批发零售 Wholesale & Retail 77 ... 3.29 102688643 2.69
7 运输仓储 Transportation 77 ... 1.13 46450606 1.22
8 住宿餐饮 Hotels & Catering 77 ... 0.25 6348476 0.17
9 信息技术 IT 77 ... 12.11 429609907 11.26
10 金融业 Finance 77 ... 3.36 102223897 2.68
11 房地产 Real Estate 77 ... 6.47 132471503 3.47
12 商务服务 Business Support 77 ... 2.72 73174343 1.92
13 科研服务 Research & Development 77 ... 1.06 58211379 1.53
14 公共环保 Environmental Protection 77 ... 1.90 59623129 1.56
15 居民服务 Resident Services 77 ... 0.00 280137 0.01
16 教育 Education 77 ... 0.39 11672172 0.31
17 卫生 Public Health 77 ... 0.55 20643720 0.54
18 文化传播 Media 77 ... 2.42 70405699 1.84
19 综合 Conglomerates 77 ... 0.26 8810109 0.23
```

##### 上海证券交易所-每日概况

接口: stock_sse_deal_daily
Expand Down
1 change: 1 addition & 0 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@
# 股票市场总貌
"stock_szse_summary" # 深圳证券交易所-市场总貌-证券类别统计
"stock_szse_area_summary" # 深圳证券交易所-市场总貌-地区交易排序
"stock_szse_sector_summary" # 深圳证券交易所-统计资料-股票行业成交
"stock_sse_summary" # 上海证券交易所-股票数据总貌
"stock_sse_deal_daily" # 上海证券交易所-每日股票情况
# 美股港股目标价
Expand Down

0 comments on commit c8ab581

Please sign in to comment.