Skip to content

Commit

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

* update date

* add version

* fix stock_zh_a_daily

* format docs

* formated dosc

* fix stock_zh_a_daily

* fix stock_zh_a_daily
  • Loading branch information
albertandking authored Sep 26, 2023
1 parent 79ab7bf commit 71709b6
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 86 deletions.
5 changes: 3 additions & 2 deletions akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2528,9 +2528,10 @@
1.11.12 fix: fix stock_board_concept_hist_ths interface
1.11.13 fix: fix futures_comm_info interface
1.11.14 fix: fix migration_area_baidu interface
1.11.15 fix: fix stock_dividend_cninfo interface
"""

__version__ = "1.11.14"
__version__ = "1.11.15"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -3208,7 +3209,7 @@
"""
个股分红
"""
from akshare.stock.stock_dividents_cninfo import stock_dividents_cninfo
from akshare.stock.stock_dividend_cninfo import stock_dividend_cninfo

"""
公司股本变动
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2021/9/16 17:29
Date: 2023/9/26 15:00
Desc: 巨潮资讯-个股-历史分红
http://webapi.cninfo.com.cn/#/company?companyid=600009
"""
import time
from py_mini_racer import py_mini_racer
import requests
import pandas as pd
import requests
from py_mini_racer import py_mini_racer

from akshare.datasets import get_ths_js

js_str = """
function mcode(input) {
var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv" + "wxyz0123456789+/" + "=";
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
+ keyStr.charAt(enc3) + keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
"""

def _get_file_content_ths(file: str = "cninfo.js") -> str:
"""
获取 JS 文件的内容
:param file: JS 文件名
:type file: str
:return: 文件内容
:rtype: str
"""
setting_file_path = get_ths_js(file)
with open(setting_file_path) as f:
file_data = f.read()
return file_data


def stock_dividents_cninfo(symbol: str = "600009") -> pd.DataFrame:
def stock_dividend_cninfo(symbol: str = "600009") -> pd.DataFrame:
"""
巨潮资讯-个股-历史分红
http://webapi.cninfo.com.cn/#/company?companyid=600009
Expand All @@ -52,21 +36,19 @@ def stock_dividents_cninfo(symbol: str = "600009") -> pd.DataFrame:
:rtype: pandas.DataFrame
"""
url = "http://webapi.cninfo.com.cn/api/sysapi/p_sysapi1139"
params = {
'scode': symbol
}
random_time_str = str(int(time.time()))
params = {"scode": symbol}
js_code = py_mini_racer.MiniRacer()
js_code.eval(js_str)
mcode = js_code.call("mcode", random_time_str)
js_content = _get_file_content_ths("cninfo.js")
js_code.eval(js_content)
mcode = js_code.call("getResCode1")
headers = {
"Accept": "*/*",
"Accept-Enckey": mcode,
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Content-Length": "0",
"Host": "webapi.cninfo.com.cn",
"mcode": mcode,
"Origin": "http://webapi.cninfo.com.cn",
"Pragma": "no-cache",
"Proxy-Connection": "keep-alive",
Expand Down Expand Up @@ -101,5 +83,5 @@ def stock_dividents_cninfo(symbol: str = "600009") -> pd.DataFrame:


if __name__ == "__main__":
stock_dividents_cninfo_df = stock_dividents_cninfo(symbol="600009")
print(stock_dividents_cninfo_df)
stock_dividend_cninfo_df = stock_dividend_cninfo(symbol="600009")
print(stock_dividend_cninfo_df)
63 changes: 31 additions & 32 deletions akshare/stock/stock_zh_a_sina.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def stock_zh_a_spot() -> pd.DataFrame:
page_count = _get_zh_a_page_count()
zh_sina_stock_payload_copy = zh_sina_a_stock_payload.copy()
for page in tqdm(
range(1, page_count + 1), leave=False, desc="Please wait for a moment"
range(1, page_count + 1), leave=False, desc="Please wait for a moment"
):
zh_sina_stock_payload_copy.update({"page": page})
r = requests.get(
Expand Down Expand Up @@ -126,10 +126,10 @@ def stock_zh_a_spot() -> pd.DataFrame:


def stock_zh_a_daily(
symbol: str = "sh603843",
start_date: str = "19900101",
end_date: str = "21000118",
adjust: str = "",
symbol: str = "sh603843",
start_date: str = "19900101",
end_date: str = "21000118",
adjust: str = "",
) -> pd.DataFrame:
"""
新浪财经-A 股-个股的历史行情数据, 大量抓取容易封 IP
Expand All @@ -145,6 +145,7 @@ def stock_zh_a_daily(
:return: 行情数据
:rtype: pandas.DataFrame
"""

def _fq_factor(method: str) -> pd.DataFrame:
if method == "hfq":
res = requests.get(zh_sina_a_stock_hfq_url.format(symbol))
Expand Down Expand Up @@ -186,7 +187,7 @@ def _fq_factor(method: str) -> pd.DataFrame:
data_df = data_df.astype("float")
r = requests.get(zh_sina_a_stock_amount_url.format(symbol, symbol))
amount_data_json = demjson.decode(
r.text[r.text.find("[") : r.text.rfind("]") + 1]
r.text[r.text.find("["): r.text.rfind("]") + 1]
)
amount_data_df = pd.DataFrame(amount_data_json)
amount_data_df.index = pd.to_datetime(amount_data_df.date)
Expand All @@ -195,16 +196,14 @@ def _fq_factor(method: str) -> pd.DataFrame:
data_df, amount_data_df, left_index=True, right_index=True, how="outer"
)
try:
# try for pandas < 2.1.0
temp_df.fillna(method="ffill", inplace=True)
# try for pandas >= 2.1.0
temp_df.ffill(inplace=True)
except Exception as e:
try:
# try for pandas >= 3.6.0
temp_df.ffill(inplace=True)
temp_df.fillna(method="ffill", inplace=True)
except Exception as e:
print("Error:", e)
print("Error:", e)


temp_df = temp_df.astype(float)
temp_df["amount"] = temp_df["amount"] * 10000
temp_df["turnover"] = temp_df["volume"] / temp_df["amount"]
Expand Down Expand Up @@ -250,7 +249,7 @@ def _fq_factor(method: str) -> pd.DataFrame:
temp_df.ffill(inplace=True)
except Exception as e:
try:
# try for pandas < 2.1.0
# try for pandas < 2.1.0
temp_df.fillna(method="ffill", inplace=True)
except Exception as e:
print("Error:", e)
Expand Down Expand Up @@ -293,7 +292,7 @@ def _fq_factor(method: str) -> pd.DataFrame:
temp_df.ffill(inplace=True)
except Exception as e:
try:
# try for pandas < 2.1.0
# try for pandas < 2.1.0
temp_df.fillna(method="ffill", inplace=True)
except Exception as e:
print("Error:", e)
Expand All @@ -318,9 +317,9 @@ def _fq_factor(method: str) -> pd.DataFrame:


def stock_zh_a_cdr_daily(
symbol: str = "sh689009",
start_date: str = "19900101",
end_date: str = "22201116",
symbol: str = "sh689009",
start_date: str = "19900101",
end_date: str = "22201116",
) -> pd.DataFrame:
"""
新浪财经-A股-CDR个股的历史行情数据, 大量抓取容易封 IP
Expand Down Expand Up @@ -355,7 +354,7 @@ def stock_zh_a_cdr_daily(


def stock_zh_a_minute(
symbol: str = "sh600519", period: str = "1", adjust: str = ""
symbol: str = "sh600519", period: str = "1", adjust: str = ""
) -> pd.DataFrame:
"""
股票及股票指数历史行情数据-分钟数据
Expand Down Expand Up @@ -419,24 +418,24 @@ def stock_zh_a_minute(
stock_zh_a_daily_qfq_df.index = pd.to_datetime(
stock_zh_a_daily_qfq_df["date"]
)
result_df = stock_zh_a_daily_qfq_df.iloc[-len(need_df) :, :][
"close"
].astype(float) / need_df["close"].astype(float)
result_df = stock_zh_a_daily_qfq_df.iloc[-len(need_df):, :][
"close"
].astype(float) / need_df["close"].astype(float)
temp_df.index = pd.to_datetime(temp_df["date"])
merged_df = pd.merge(
temp_df, result_df, left_index=True, right_index=True
)
merged_df["open"] = (
merged_df["open"].astype(float) * merged_df["close_y"]
merged_df["open"].astype(float) * merged_df["close_y"]
)
merged_df["high"] = (
merged_df["high"].astype(float) * merged_df["close_y"]
merged_df["high"].astype(float) * merged_df["close_y"]
)
merged_df["low"] = (
merged_df["low"].astype(float) * merged_df["close_y"]
merged_df["low"].astype(float) * merged_df["close_y"]
)
merged_df["close"] = (
merged_df["close_x"].astype(float) * merged_df["close_y"]
merged_df["close_x"].astype(float) * merged_df["close_y"]
)
temp_df = merged_df[["day", "open", "high", "low", "close", "volume"]]
temp_df.reset_index(drop=True, inplace=True)
Expand All @@ -456,24 +455,24 @@ def stock_zh_a_minute(
stock_zh_a_daily_hfq_df.index = pd.to_datetime(
stock_zh_a_daily_hfq_df["date"]
)
result_df = stock_zh_a_daily_hfq_df.iloc[-len(need_df) :, :][
"close"
].astype(float) / need_df["close"].astype(float)
result_df = stock_zh_a_daily_hfq_df.iloc[-len(need_df):, :][
"close"
].astype(float) / need_df["close"].astype(float)
temp_df.index = pd.to_datetime(temp_df["date"])
merged_df = pd.merge(
temp_df, result_df, left_index=True, right_index=True
)
merged_df["open"] = (
merged_df["open"].astype(float) * merged_df["close_y"]
merged_df["open"].astype(float) * merged_df["close_y"]
)
merged_df["high"] = (
merged_df["high"].astype(float) * merged_df["close_y"]
merged_df["high"].astype(float) * merged_df["close_y"]
)
merged_df["low"] = (
merged_df["low"].astype(float) * merged_df["close_y"]
merged_df["low"].astype(float) * merged_df["close_y"]
)
merged_df["close"] = (
merged_df["close_x"].astype(float) * merged_df["close_y"]
merged_df["close_x"].astype(float) * merged_df["close_y"]
)
temp_df = merged_df[["day", "open", "high", "low", "close", "volume"]]
temp_df.reset_index(drop=True, inplace=True)
Expand Down
7 changes: 4 additions & 3 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/8/22 14:20
Date: 2023/9/25 15:20
Desc: 东方财富-股票-财务分析
"""
from functools import lru_cache
Expand Down Expand Up @@ -53,7 +53,7 @@ def stock_balance_sheet_by_report_em(symbol: str = "SH600519") -> pd.DataFrame:
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)]
big_df = pd.DataFrame()
big_list = list()
for item in tqdm(sep_list, leave=False):
url = "https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/zcfzbAjaxNew"
params = {
Expand All @@ -66,7 +66,8 @@ def stock_balance_sheet_by_report_em(symbol: str = "SH600519") -> pd.DataFrame:
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
big_df = pd.concat([big_df, temp_df], ignore_index=True)
big_list.append(temp_df)
big_df = pd.concat(big_list, ignore_index=True)
return big_df


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

## 更新说明详情

1.11.15 fix: fix stock_dividend_cninfo interface

1. 修复 stock_dividend_cninfo 接口
2. 修复 stock_zh_a_daily 接口

1.11.14 fix: fix migration_area_baidu interface

1. 修复 migration_area_baidu 接口
Expand Down Expand Up @@ -2887,6 +2892,8 @@

## 版本更新说明

1.11.15 fix: fix stock_dividend_cninfo interface

1.11.14 fix: fix migration_area_baidu interface

1.11.13 fix: fix futures_comm_info interface
Expand Down
2 changes: 1 addition & 1 deletion docs/data/event/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

描述: 百度-百度地图慧眼-百度迁徙-迁入/迁出地数据接口

限量: 单次返回前 50 个城市, 由于百度接口限制, 目前只能返回前 50 个城市
限量: 单次返回前 100 个城市的数据

输入参数

Expand Down
6 changes: 3 additions & 3 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -11941,7 +11941,7 @@ print(stock_history_dividend_detail_df)

#### 历史分红

接口: stock_dividents_cninfo
接口: stock_dividend_cninfo

目标地址: http://webapi.cninfo.com.cn/#/company?companyid=600009

Expand Down Expand Up @@ -11976,8 +11976,8 @@ print(stock_history_dividend_detail_df)
```python
import akshare as ak

stock_dividents_cninfo_df = ak.stock_dividents_cninfo(symbol="600009")
print(stock_dividents_cninfo_df)
stock_dividend_cninfo_df = ak.stock_dividend_cninfo(symbol="600009")
print(stock_dividend_cninfo_df)
```

数据示例
Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**风险提示:[AKShare](https://github.com/akfamily/akshare) 开源财经数据接口库所采集的数据皆来自公开的数据源,不涉及任何个人隐私数据和非公开数据。
同时本项目提供的数据接口及相关数据仅用于学术研究,任何个人、机构及团体使用本项目的数据接口及相关数据请注意商业风险。**

1. 本文档更新时间:**2023-09-24**
1. 本文档更新时间:**2023-09-26**
2. 如有 [AKShare](https://github.com/akfamily/akshare) 库、文档及数据的相关问题,请在 [AKShare Issues](https://github.com/akfamily/akshare/issues) 中提 Issues;
3. 欢迎关注 **数据科学实战** 微信公众号:<div><img src="https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/readme/qrcode/ds.png"></div>;
4. 如果您的问题未能在文档中找到答案,您也可以加入 **AKShare-VIP QQ 群**: 为了提高问答质量,此群为收费群(一杯咖啡钱即可入群,赠送[《AKShare-初阶-使用教学》](https://zmj.xet.tech/s/wck86)视频课),可以添加 **AKShare-小助手** QQ:1254836886,由小助手邀请入群! ![](https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/readme/qrcode/qr_code_1254836886.jpg)
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
"stock_ipo_info" # 股票新股
"stock_history_dividend_detail" # 分红配股
"stock_history_dividend" # 历史分红
"stock_dividents_cninfo" # 个股历史分红
"stock_dividend_cninfo" # 个股历史分红
"stock_restricted_release_queue_sina" # 限售解禁-新浪
"stock_restricted_release_summary_em" # 东方财富网-数据中心-特色数据-限售股解禁
"stock_restricted_release_detail_em" # 东方财富网-数据中心-限售股解禁-解禁详情一览
Expand Down

0 comments on commit 71709b6

Please sign in to comment.