Skip to content

Commit

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

* fix get_dce_rank_table

* add stock_board_concept_graph_ths
  • Loading branch information
albertandking authored Oct 14, 2023
1 parent d6ef6d0 commit e20c2ab
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 37 deletions.
4 changes: 3 additions & 1 deletion akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,9 +2547,10 @@
1.11.31 fix: fix futures_dce_warehouse_receipt interface
1.11.32 add: add stock_margin_ratio_pa interface
1.11.33 add: add stock_intraday_em interface
1.11.34 add: add stock_board_concept_graph_ths interface
"""

__version__ = "1.11.33"
__version__ = "1.11.34"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -3636,6 +3637,7 @@
stock_board_concept_name_ths,
stock_board_concept_info_ths,
stock_board_concept_hist_ths,
stock_board_concept_graph_ths,
stock_board_cons_ths,
)

Expand Down
1 change: 1 addition & 0 deletions akshare/futures/cot.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ def get_dce_rank_table(date: str = "20230706", vars_list=cons.contract_symbols):
var 品种 string
date 日期 string YYYYMMDD
"""
print("如果本接口不可用,请使用 ak.futures_dce_position_rank() 接口")
date_string = date
date = cons.convert_date(date) if date is not None else datetime.date.today()
if date < datetime.date(2006, 1, 4):
Expand Down
85 changes: 52 additions & 33 deletions akshare/stock_feature/stock_board_concept_ths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/9/19 12:10
Date: 2023/10/14 22:10
Desc: 同花顺-板块-概念板块
http://q.10jqka.com.cn/gn/detail/code/301558/
"""
Expand All @@ -19,6 +19,42 @@
from akshare.utils import demjson


def stock_board_concept_graph_ths(symbol: str = "通用航空") -> pd.DataFrame:
"""
同花顺-板块-概念板块-概念图谱
http://q.10jqka.com.cn/gn/detail/code/301558/
:param symbol: 板块名称
:type symbol: str
:return: 概念图谱
:rtype: pandas.DataFrame
"""
stock_board_ths_map_df = stock_board_concept_name_ths()
symbol = (
stock_board_ths_map_df[stock_board_ths_map_df["概念名称"] == symbol]["网址"]
.values[0]
.split("/")[-2]
)
url = f"http://q.10jqka.com.cn/gn/detail/code/{symbol}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
}
r = requests.get(url, headers=headers)
temp_df = pd.read_html(StringIO(r.text))[0]
new_list = []
for col in temp_df.columns:
temp_list = temp_df[col].values[0].split(" ")
for i, item in enumerate(temp_list):
if i % 2 != 0:
price_pct, pct = item.split(" ")
price_pct = price_pct.strip("%").strip("+").strip("-")
pct = pct.strip("-").strip("+")
new_list.append([col, temp_list[i - 1], price_pct, pct])
temp_df = pd.DataFrame(new_list, columns=["产业链", "名称", "涨跌幅", "现价"])
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["现价"] = pd.to_numeric(temp_df["现价"], errors="coerce")
return temp_df


def _get_file_content_ths(file: str = "ths.js") -> str:
"""
获取 JS 文件的内容
Expand Down Expand Up @@ -52,9 +88,7 @@ def stock_board_concept_name_ths() -> pd.DataFrame:
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
total_page = soup.find("span", attrs={"class": "page_info"}).text.split(
"/"
)[1]
total_page = soup.find("span", attrs={"class": "page_info"}).text.split("/")[1]
big_df = pd.DataFrame()
for page in tqdm(range(1, int(total_page) + 1), leave=False):
url = f"http://q.10jqka.com.cn/gn/index/field/addtime/order/desc/page/{page}/ajax/1/"
Expand Down Expand Up @@ -91,8 +125,7 @@ def stock_board_concept_name_ths() -> pd.DataFrame:
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
need_list = [
item.find_all("a")
for item in soup.find_all(attrs={"class": "cate_group"})
item.find_all("a") for item in soup.find_all(attrs={"class": "cate_group"})
]
temp_list = []
for item in need_list:
Expand All @@ -106,9 +139,7 @@ def stock_board_concept_name_ths() -> pd.DataFrame:
temp_df.columns = ["概念名称", "网址"]
temp_df["日期"] = None
temp_df["成分股数量"] = None
temp_df["代码"] = (
temp_df["网址"].str.split("/", expand=True).iloc[:, 6].tolist()
)
temp_df["代码"] = temp_df["网址"].str.split("/", expand=True).iloc[:, 6].tolist()
temp_df = temp_df[["日期", "概念名称", "成分股数量", "网址", "代码"]]
big_df = pd.concat([big_df, temp_df], ignore_index=True)
big_df.drop_duplicates(subset=["概念名称"], keep="first", inplace=True)
Expand All @@ -125,8 +156,7 @@ def _stock_board_concept_code_ths() -> dict:
_stock_board_concept_name_ths_df = stock_board_concept_name_ths()
name_list = _stock_board_concept_name_ths_df["概念名称"].tolist()
url_list = [
item.split("/")[-2]
for item in _stock_board_concept_name_ths_df["网址"].tolist()
item.split("/")[-2] for item in _stock_board_concept_name_ths_df["网址"].tolist()
]
temp_map = dict(zip(name_list, url_list))
return temp_map
Expand Down Expand Up @@ -159,9 +189,7 @@ def stock_board_concept_cons_ths(symbol: str = "阿里巴巴概念") -> pd.DataF
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
try:
page_num = int(
soup.find_all("a", attrs={"class": "changePage"})[-1]["page"]
)
page_num = int(soup.find_all("a", attrs={"class": "changePage"})[-1]["page"])
except IndexError as e:
page_num = 1
big_df = pd.DataFrame()
Expand Down Expand Up @@ -214,15 +242,11 @@ def stock_board_concept_info_ths(symbol: str = "阿里巴巴概念") -> pd.DataF
soup = BeautifulSoup(r.text, "lxml")
name_list = [
item.text
for item in soup.find("div", attrs={"class": "board-infos"}).find_all(
"dt"
)
for item in soup.find("div", attrs={"class": "board-infos"}).find_all("dt")
]
value_list = [
item.text.strip().replace("\n", "/")
for item in soup.find("div", attrs={"class": "board-infos"}).find_all(
"dd"
)
for item in soup.find("div", attrs={"class": "board-infos"}).find_all("dd")
]
temp_df = pd.DataFrame([name_list, value_list]).T
temp_df.columns = ["项目", "值"]
Expand All @@ -249,9 +273,7 @@ def stock_board_concept_hist_ths(
}
r = requests.get(symbol_url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
symbol_code = (
soup.find("div", attrs={"class": "board-hq"}).find("span").text
)
symbol_code = soup.find("div", attrs={"class": "board-hq"}).find("span").text
big_df = pd.DataFrame()
current_year = datetime.now().year
for year in tqdm(range(int(start_year), current_year + 1), leave=False):
Expand Down Expand Up @@ -349,9 +371,7 @@ def stock_board_cons_ths(symbol: str = "301558") -> pd.DataFrame:
soup = BeautifulSoup(r.text, "lxml")
url_flag = "gn"
try:
page_num = int(
soup.find_all("a", attrs={"class": "changePage"})[-1]["page"]
)
page_num = int(soup.find_all("a", attrs={"class": "changePage"})[-1]["page"])
except IndexError as e:
page_num = 1
big_df = pd.DataFrame()
Expand Down Expand Up @@ -381,21 +401,20 @@ def stock_board_cons_ths(symbol: str = "301558") -> pd.DataFrame:


if __name__ == "__main__":
stock_board_concept_graph_ths_df = stock_board_concept_graph_ths(symbol="通用航空")
print(stock_board_concept_graph_ths_df)

stock_board_concept_name_ths_df = stock_board_concept_name_ths()
print(stock_board_concept_name_ths_df)

stock_board_concept_cons_ths_df = stock_board_concept_cons_ths(
symbol="小米概念"
)
stock_board_concept_cons_ths_df = stock_board_concept_cons_ths(symbol="小米概念")
print(stock_board_concept_cons_ths_df)

stock_board_concept_info_ths_df = stock_board_concept_info_ths(
symbol="PVDF概念"
)
stock_board_concept_info_ths_df = stock_board_concept_info_ths(symbol="PVDF概念")
print(stock_board_concept_info_ths_df)

stock_board_concept_hist_ths_df = stock_board_concept_hist_ths(
start_year="2022", symbol="阿里巴巴概念"
start_year="2023", symbol="新能源汽车"
)
print(stock_board_concept_hist_ths_df)

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

## 更新说明详情

1.11.34 add: add stock_board_concept_graph_ths interface

1. 新增 stock_board_concept_graph_ths 接口

1.11.33 add: add stock_intraday_em interface

1. 新增 stock_intraday_em 接口
Expand Down Expand Up @@ -2964,6 +2968,8 @@

## 版本更新说明

1.11.34 add: add stock_board_concept_graph_ths interface

1.11.33 add: add stock_intraday_em interface

1.11.32 add: add stock_margin_ratio_pa interface
Expand Down
4 changes: 2 additions & 2 deletions docs/data/futures/futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,8 @@ ak.get_shfe_rank_table()
```python
import akshare as ak

get_dce_rank_table_df = ak.get_czce_rank_table(date='20200213')
print(get_dce_rank_table_df)
get_czce_rank_table_df = ak.get_czce_rank_table(date='20200213')
print(get_czce_rank_table_df)
```

注意:
Expand Down
55 changes: 55 additions & 0 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -18193,6 +18193,61 @@ print(stock_board_concept_cons_ths_df)
40 41 688088 虹软科技 51.58 ... 0.96亿 2.64亿 136.19亿 80.110000
```

#### 同花顺-板块-概念板块-概念图谱

接口: stock_board_concept_graph_ths

目标地址: http://q.10jqka.com.cn/gn/detail/code/301558/

描述: 同花顺-板块-概念板块-概念图谱

限量: 单次返回指定 symbol 的概念图谱数据; 部分板块暂无概念图谱数据

输入参数

| 名称 | 类型 | 描述 |
|--------|-----|-------------------------------------------------------------------------------------------------------------------|
| symbol | str | symbol="阿里巴巴概念"; 可以通过调用 **ak.stock_board_concept_name_ths()** 查看同花顺的所有概念名称; 注意:其中 "国家大基金持股" 为 "国家大基金持股 ", 最后有一个空格 |

输出参数

| 名称 | 类型 | 描述 |
|-----|---------|----|
| 产业链 | object | - |
| 名称 | object | - |
| 涨跌幅 | float64 | - |
| 现价 | float64 | - |

接口示例

```python
import akshare as ak

stock_board_concept_graph_ths_df = ak.stock_board_concept_graph_ths(symbol="通用航空")
print(stock_board_concept_graph_ths_df)
```

数据示例

```
产业链 名称 涨跌幅 现价
0 设备制造 中航高科 0.00 24.60
1 设备制造 德奥退 0.00 0.53
2 设备制造 四创电子 0.46 21.84
3 设备制造 贵航股份 0.60 13.17
4 设备制造 威海广泰 0.75 9.30
5 设备制造 南山铝业 0.95 3.13
6 设备制造 航新科技 0.99 13.05
7 设备制造 川大智胜 1.08 13.70
8 设备制造 通裕重工 1.17 2.53
9 航空运营 山河智能 6.35 6.70
10 航空运营 海特高新 0.70 9.93
11 航空运营 中信海直 1.52 8.41
12 航空运营 华夏幸福 2.48 1.97
13 其他 中化岩土 1.48 2.74
14 其他 应流股份 0.00 13.98
```

#### 同花顺-成份股-行业板块和概念板块

接口: stock_board_cons_ths
Expand Down
4 changes: 3 additions & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"get_gfex_daily", # 获取广州期货交易所每日交易数据
"get_ine_daily", # 获取上海国际能源交易中心每日交易数据
"futures_sgx_daily", # 获取新加坡交易所每日交易数据
"get_dce_rank_table", #获取大连商品交易所前20会员持仓数据明细
"get_dce_rank_table", # 获取大连商品交易所前20会员持仓数据明细
"get_futures_daily", # 获取中国金融期货交易所每日基差数据
"get_rank_sum", # 获取四个期货交易所前5, 10, 15, 20会员持仓排名数据
"get_rank_sum_daily", # 获取每日四个期货交易所前5, 10, 15, 20会员持仓排名数据
Expand Down Expand Up @@ -1216,6 +1216,8 @@
"stock_margin_ratio_pa" # 融资融券-标的证券名单及保证金比例查询
# 东财财富-日内分时数据
"stock_intraday_em" # 东财财富-日内分时数据
# 同花顺-板块-概念板块-概念图谱
"stock_board_concept_graph_ths" # 同花顺-板块-概念板块-概念图谱
```

## 案例演示
Expand Down

0 comments on commit e20c2ab

Please sign in to comment.