From ae3c9376234190f93e91ea3c8c5786eac2dcf2b9 Mon Sep 17 00:00:00 2001 From: Albert King Date: Tue, 30 Jan 2024 17:13:16 +0800 Subject: [PATCH] Date (#4479) * add version 1.12.47 * fix get_receipt * fix stock_zh_ah_daily * update date * fix get_receipt * add version 1.12.47 --- akshare/__init__.py | 3 +- akshare/futures/receipt.py | 48 ++++++++++++----------- akshare/stock/stock_zh_ah_tx.py | 37 +++++++++--------- docs/changelog.md | 7 ++++ docs/data/futures/futures.md | 2 +- docs/data/stock/stock.md | 67 +++++++++++++++++---------------- docs/introduction.md | 2 +- 7 files changed, 91 insertions(+), 75 deletions(-) diff --git a/akshare/__init__.py b/akshare/__init__.py index 665c7e573d2..91854506804 100644 --- a/akshare/__init__.py +++ b/akshare/__init__.py @@ -2659,9 +2659,10 @@ 1.12.44 fix: fix stock_zh_ah_daily interface 1.12.45 fix: fix stock_individual_spot_xq interface 1.12.46 fix: fix futures_contract_detail interface +1.12.47 fix: fix stock_zh_ah_daily interface """ -__version__ = "1.12.46" +__version__ = "1.12.47" __author__ = "AKFamily" import sys diff --git a/akshare/futures/receipt.py b/akshare/futures/receipt.py index 764e917af01..e67dceec368 100644 --- a/akshare/futures/receipt.py +++ b/akshare/futures/receipt.py @@ -385,9 +385,11 @@ def get_gfex_receipt(date: str = None, vars_list: List = cons.contract_symbols) temp_df = pd.DataFrame(data_json['data']) temp_df = temp_df[temp_df['variety'].str.contains("小计")] result_df = temp_df[['wbillQty', 'diff']].copy() + if result_df.empty: + return pd.DataFrame() + result_df.loc[:, 'date'] = date.isoformat().replace("-", "") result_df.loc[:, 'var'] = [item.upper() for item in temp_df['varietyOrder'].tolist()] - result_df.reset_index(drop=True, inplace=True) result_df.rename(columns={ "wbillQty": "receipt", @@ -397,18 +399,20 @@ def get_gfex_receipt(date: str = None, vars_list: List = cons.contract_symbols) 'var', 'receipt', 'receipt_chg', 'date' ]] result_df.set_index(['var'], inplace=True) + if 'LC' not in result_df.index: + vars_list.remove('LC') result_df = result_df.loc[vars_list, :] result_df.reset_index(inplace=True) return result_df -def get_receipt(start_day: str = None, end_day: str = None, vars_list: List = cons.contract_symbols): +def get_receipt(start_date: str = None, end_date: str = None, vars_list: List = cons.contract_symbols): """ 大宗商品-注册仓单数据 - :param start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天 - :type start_day: str - :param end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天 - :type end_day: str + :param start_date: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天 + :type start_date: str + :param end_date: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天 + :type end_date: str :param vars_list: 合约品种如 RB、AL 等列表为空时为所有商品 :type vars_list: str :return: 展期收益率数据 @@ -416,42 +420,42 @@ def get_receipt(start_day: str = None, end_day: str = None, vars_list: List = co """ if not isinstance(vars_list, list): return warnings.warn(f"vars_list: 必须是列表") - start_day = cons.convert_date(start_day) if start_day is not None else datetime.date.today() - end_day = cons.convert_date(end_day) if end_day is not None else cons.convert_date( + start_date = cons.convert_date(start_date) if start_date is not None else datetime.date.today() + end_date = cons.convert_date(end_date) if end_date is not None else cons.convert_date( cons.get_latest_data_date(datetime.datetime.now())) records = pd.DataFrame() - while start_day <= end_day: - if start_day.strftime('%Y%m%d') not in calendar: - warnings.warn(f"{start_day.strftime('%Y%m%d')} 非交易日") + while start_date <= end_date: + if start_date.strftime('%Y%m%d') not in calendar: + warnings.warn(f"{start_date.strftime('%Y%m%d')} 非交易日") else: - print(start_day) + print(start_date) for market, market_vars in cons.market_exchange_symbols.items(): if market == 'dce': - if start_day >= datetime.date(2009, 4, 7): + if start_date >= datetime.date(2009, 4, 7): f = get_dce_receipt else: print('20090407 起,大连商品交易所每个交易日更新仓单数据') f = None elif market == 'shfe': - if datetime.date(2008, 10, 6) <= start_day <= datetime.date(2014, 5, 16): + if datetime.date(2008, 10, 6) <= start_date <= datetime.date(2014, 5, 16): f = get_shfe_receipt_1 - elif start_day > datetime.date(2014, 5, 16): + elif start_date > datetime.date(2014, 5, 16): f = get_shfe_receipt_2 else: f = None print('20081006 起,上海期货交易所每个交易日更新仓单数据') elif market == "gfex": - if start_day > datetime.date(2022, 12, 22): + if start_date > datetime.date(2022, 12, 22): f = get_gfex_receipt else: f = None print('20081006 起,上海期货交易所每个交易日更新仓单数据') elif market == 'czce': - if datetime.date(2008, 3, 3) <= start_day <= datetime.date(2010, 8, 24): + if datetime.date(2008, 3, 3) <= start_date <= datetime.date(2010, 8, 24): f = get_czce_receipt_1 - elif datetime.date(2010, 8, 24) < start_day <= datetime.date(2015, 11, 11): + elif datetime.date(2010, 8, 24) < start_date <= datetime.date(2015, 11, 11): f = get_czce_receipt_2 - elif start_day > datetime.date(2015, 11, 11): + elif start_date > datetime.date(2015, 11, 11): f = get_czce_receipt_3 else: f = None @@ -459,8 +463,8 @@ def get_receipt(start_day: str = None, end_day: str = None, vars_list: List = co get_vars = [var for var in vars_list if var in market_vars] if market != 'cffex' and get_vars != []: if f is not None: - records = pd.concat([records, f(start_day, get_vars)]) - start_day += datetime.timedelta(days=1) + records = pd.concat([records, f(start_date, get_vars)]) + start_date += datetime.timedelta(days=1) records.reset_index(drop=True, inplace=True) if records.empty: return records @@ -468,5 +472,5 @@ def get_receipt(start_day: str = None, end_day: str = None, vars_list: List = co if __name__ == '__main__': - get_receipt_df = get_receipt(start_day='20240122', end_day='20240122', vars_list=['LC']) + get_receipt_df = get_receipt(start_date='20230601', end_date='20230615') print(get_receipt_df) diff --git a/akshare/stock/stock_zh_ah_tx.py b/akshare/stock/stock_zh_ah_tx.py index 2dddb143ee5..2e11742568e 100644 --- a/akshare/stock/stock_zh_ah_tx.py +++ b/akshare/stock/stock_zh_ah_tx.py @@ -7,10 +7,8 @@ """ import random -import requests import pandas as pd -from akshare.utils import demjson -from akshare.utils.tqdm import get_tqdm +import requests from akshare.stock.cons import ( hk_url, @@ -19,6 +17,8 @@ hk_stock_headers, hk_stock_payload, ) +from akshare.utils import demjson +from akshare.utils.tqdm import get_tqdm def _get_zh_stock_ah_page_count() -> int: @@ -52,10 +52,10 @@ def stock_zh_ah_spot() -> pd.DataFrame: hk_payload.update({"reqPage": i}) r = requests.get(hk_url, params=hk_payload, headers=hk_headers) data_json = demjson.decode( - r.text[r.text.find("{") : r.text.rfind("}") + 1] + r.text[r.text.find("{"): r.text.rfind("}") + 1] ) big_df = pd.concat( - [ + objs=[ big_df, pd.DataFrame(data_json["data"]["page_data"]) .iloc[:, 0] @@ -110,11 +110,12 @@ def stock_zh_ah_spot() -> pd.DataFrame: return big_df -def stock_zh_ah_name() -> dict: +def stock_zh_ah_name() -> pd.DataFrame: """ 腾讯财经-港股-AH-股票名称 + https://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH :return: 股票代码和股票名称的字典 - :rtype: dict + :rtype: pandas.DataFrame """ big_df = pd.DataFrame() page_count = _get_zh_stock_ah_page_count() + 1 @@ -123,10 +124,10 @@ def stock_zh_ah_name() -> dict: hk_payload.update({"reqPage": i}) r = requests.get(hk_url, params=hk_payload, headers=hk_headers) data_json = demjson.decode( - r.text[r.text.find("{") : r.text.rfind("}") + 1] + r.text[r.text.find("{"): r.text.rfind("}") + 1] ) big_df = pd.concat( - [ + objs=[ big_df, pd.DataFrame(data_json["data"]["page_data"]) .iloc[:, 0] @@ -159,10 +160,10 @@ def stock_zh_ah_name() -> dict: def stock_zh_ah_daily( - symbol: str = "02318", - start_year: str = "2000", - end_year: str = "2019", - adjust: str = "", + symbol: str = "02318", + start_year: str = "2000", + end_year: str = "2019", + adjust: str = "", ) -> pd.DataFrame: """ 腾讯财经-港股-AH-股票历史行情 @@ -210,18 +211,18 @@ def stock_zh_ah_daily( "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36", } r = requests.get( - "http://web.ifzq.gtimg.cn/appstock/app/kline/kline", + url="http://web.ifzq.gtimg.cn/appstock/app/kline/kline", params=hk_stock_payload_copy, headers=headers, ) else: r = requests.get( - "https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get", + url="https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get", params=hk_stock_payload_copy, headers=hk_stock_headers, ) data_json = demjson.decode( - r.text[r.text.find("{") : r.text.rfind("}") + 1] + r.text[r.text.find("{"): r.text.rfind("}") + 1] ) try: if adjust == "": @@ -251,7 +252,7 @@ def stock_zh_ah_daily( except: temp_df.columns = ["日期", "开盘", "收盘", "最高", "最低", "成交量"] temp_df = temp_df[["日期", "开盘", "收盘", "最高", "最低", "成交量"]] - big_df = pd.concat([big_df, temp_df], ignore_index=True) + big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True) big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date big_df["开盘"] = pd.to_numeric(big_df["开盘"], errors="coerce") big_df["收盘"] = pd.to_numeric(big_df["收盘"], errors="coerce") @@ -269,6 +270,6 @@ def stock_zh_ah_daily( print(stock_zh_ah_name_df) stock_zh_ah_daily_df = stock_zh_ah_daily( - symbol="00241", start_year="2022", end_year="2024", adjust="qfq" + symbol="02318", start_year="2022", end_year="2024", adjust="" ) print(stock_zh_ah_daily_df) diff --git a/docs/changelog.md b/docs/changelog.md index 0ec23e6340d..1c683a36b81 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -71,6 +71,11 @@ ## 更新说明详情 +1.12.47 fix: fix stock_zh_ah_daily interface + + 1. 修复 stock_zh_ah_daily 接口 + 2. 修复 get_receipt 接口 + 1.12.46 fix: fix futures_contract_detail interface 1. 修复 futures_contract_detail 接口 @@ -3481,6 +3486,8 @@ ## 版本更新说明 +1.12.47 fix: fix stock_zh_ah_daily interface + 1.12.46 fix: fix futures_contract_detail interface 1.12.45 fix: fix stock_individual_spot_xq interface diff --git a/docs/data/futures/futures.md b/docs/data/futures/futures.md index 2a8f296a5ff..3886c98fa84 100644 --- a/docs/data/futures/futures.md +++ b/docs/data/futures/futures.md @@ -851,7 +851,7 @@ ak.get_roll_yield(date="20180718", var="IF", symbol1="IF1812", symbol2="IF1811") ```python import akshare as ak -ak.get_receipt(start_day="20180712", end_day="20180719", vars_list=["CU", "NI"]) +ak.get_receipt(start_date="20180712", end_date="20180719", vars_list=["CU", "NI"]) ``` 注意: diff --git a/docs/data/stock/stock.md b/docs/data/stock/stock.md index 0c4f4577e45..1383154c531 100644 --- a/docs/data/stock/stock.md +++ b/docs/data/stock/stock.md @@ -3001,11 +3001,11 @@ print(stock_zh_ah_spot_df) 接口: stock_zh_ah_daily -目标地址: http://gu.qq.com/hk02359/gp(示例) +目标地址: https://gu.qq.com/hk02359/gp -描述: A+H 股数据是从腾讯财经获取的数据, 历史数据按日频率更新 +描述: 腾讯财经-A+H 股数据 -限量: 单次返回所有 A+H 上市公司的历史行情数据 +限量: 单次返回指定参数的 A+H 上市公司的历史行情数据 输入参数 @@ -3032,32 +3032,33 @@ print(stock_zh_ah_spot_df) ```python import akshare as ak -stock_zh_ah_daily_df = ak.stock_zh_ah_daily(symbol="02318", start_year="2000", end_year="2019", adjust="") +stock_zh_ah_daily_df = ak.stock_zh_ah_daily(symbol="02318", start_year="2022", end_year="2024", adjust="") print(stock_zh_ah_daily_df) ``` 数据示例 ``` - 日期 开盘 收盘 最高 最低 成交量 -0 2018-12-13 67.000 68.000 68.700 64.950 20213983.000 -1 2018-12-14 68.000 68.000 68.100 67.000 2579900.000 -2 2018-12-17 68.000 68.000 68.500 67.600 1131467.000 -3 2018-12-18 68.000 67.900 68.000 67.250 961400.000 -4 2018-12-19 67.850 67.500 68.000 66.400 2965300.000 -.. ... ... ... ... ... ... -225 2019-10-30 121.690 119.100 122.460 118.260 621131.000 -226 2019-10-31 119.660 133.240 136.880 119.660 4659080.000 -227 2019-11-01 132.470 131.560 134.640 129.180 1424212.000 -228 2019-11-04 135.690 136.460 137.860 132.820 1298760.000 -229 2019-11-05 136.530 134.010 138.210 132.470 868180.000 + 日期 开盘 收盘 最高 最低 成交量 +0 2022-01-03 56.80 55.75 56.95 55.60 7231921.0 +1 2022-01-04 55.95 56.50 56.95 55.75 17975862.0 +2 2022-01-05 57.50 56.80 58.00 56.60 28768656.0 +3 2022-01-06 56.90 58.00 58.00 56.90 15464523.0 +4 2022-01-07 58.00 61.10 61.10 57.55 46904651.0 +.. ... ... ... ... ... ... +753 2024-01-23 30.00 31.30 31.85 29.60 69397766.0 +754 2024-01-24 31.75 33.00 33.35 31.10 79928025.0 +755 2024-01-25 33.50 34.60 34.75 32.85 86827346.0 +756 2024-01-26 34.15 33.75 34.70 33.35 56539336.0 +757 2024-01-29 34.05 33.85 34.90 33.75 41424070.0 +[758 rows x 6 columns] ``` #### A+H股票字典 接口: stock_zh_ah_name -目标地址: http://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH +目标地址: https://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH 描述: A+H 股数据是从腾讯财经获取的数据, 历史数据按日频率更新 @@ -3081,24 +3082,26 @@ print(stock_zh_ah_daily_df) ```python import akshare as ak -stock_zh_ah_name_dict = ak.stock_zh_ah_name() -print(stock_zh_ah_name_dict) +stock_zh_ah_name_df = ak.stock_zh_ah_name() +print(stock_zh_ah_name_df) ``` 数据示例 ``` -0 01211 比亚迪股份 -1 01880 中国中免 -2 06821 凯莱英 -3 06160 百济神州 -4 09696 天齐锂业 -.. ... ... -145 01053 重庆钢铁股份 -146 02880 辽港股份 -147 01033 中石化油服 -148 00042 东北电气 -149 06116 拉夏贝尔 + 代码 名称 +0 01211 比亚迪股份 +1 06160 百济神州 +2 01880 中国中免 +3 00941 中国移动 +4 06821 凯莱英 +.. ... ... +144 01053 重庆钢铁股份 +145 00588 北京北辰实业股份 +146 02009 金隅集团 +147 02880 辽港股份 +148 01033 中石化油服 +[149 rows x 2 columns] ``` ### 美股 @@ -3107,7 +3110,7 @@ print(stock_zh_ah_name_dict) 接口: stock_us_spot_em -目标地址: http://quote.eastmoney.com/center/gridlist.html#us_stocks +目标地址: https://quote.eastmoney.com/center/gridlist.html#us_stocks 描述: 美股实时行情数据, 从东方财富获取; 注意数据有 15 分钟延迟 @@ -3170,7 +3173,7 @@ print(stock_us_spot_em_df) 接口: stock_us_spot -目标地址: http://finance.sina.com.cn/stock/usstock/sector.shtml +目标地址: https://finance.sina.com.cn/stock/usstock/sector.shtml 描述: 新浪财经-美股; 获取的数据有 15 分钟延迟; 建议使用 ak.stock_us_spot_em() 来获取数据 diff --git a/docs/introduction.md b/docs/introduction.md index 363d833aaa0..9764c86bc27 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -6,7 +6,7 @@ **风险提示:[AKShare](https://github.com/akfamily/akshare) 开源财经数据接口库所采集的数据皆来自公开的数据源,不涉及任何个人隐私数据和非公开数据。 同时本项目提供的数据接口及相关数据仅用于学术研究,任何个人、机构及团体使用本项目的数据接口及相关数据请注意商业风险。** -1. 本文档更新时间:**2024-01-29**; +1. 本文档更新时间:**2024-01-30**; 2. 如有 [AKShare](https://github.com/akfamily/akshare) 库、文档及数据的相关问题,请在 [AKShare Issues](https://github.com/akfamily/akshare/issues) 中提 Issues; 3. 欢迎关注 **数据科学实战** 微信公众号:
; 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)