From d6ef6d0a74a12d0e99f16a5be73ed4dba704e22f Mon Sep 17 00:00:00 2001 From: Albert King Date: Sat, 14 Oct 2023 22:11:44 +0800 Subject: [PATCH] Date (#4191) * add version * fix stock_intraday_em * update date --- akshare/__init__.py | 8 +- akshare/stock/stock_intraday_em.py | 145 +++++++++++++++++++++++++++++ docs/changelog.md | 6 ++ docs/data/stock/stock.md | 51 ++++++++++ docs/introduction.md | 2 +- docs/tutorial.md | 2 + 6 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 akshare/stock/stock_intraday_em.py diff --git a/akshare/__init__.py b/akshare/__init__.py index c964917d211..4bbee6591eb 100644 --- a/akshare/__init__.py +++ b/akshare/__init__.py @@ -2546,9 +2546,10 @@ 1.11.30 fix: fix stock_zh_a_st_em interface 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 """ -__version__ = "1.11.32" +__version__ = "1.11.33" __author__ = "AKFamily" import sys @@ -2563,6 +2564,11 @@ del sys +""" +东财财富-分时数据 +""" +from akshare.stock.stock_intraday_em import stock_intraday_em + """ 美股指数行情 """ diff --git a/akshare/stock/stock_intraday_em.py b/akshare/stock/stock_intraday_em.py new file mode 100644 index 00000000000..3a2820b39fb --- /dev/null +++ b/akshare/stock/stock_intraday_em.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +""" +Date: 2023/10/14 22:00 +Desc: 东财财富-日内分时数据 +https://quote.eastmoney.com/f1.html?newcode=0.000001 +""" +import json +from functools import lru_cache + +import pandas as pd +import requests + + +@lru_cache() +def __code_id_map_em() -> dict: + """ + 东方财富-股票和市场代码 + https://quote.eastmoney.com/center/gridlist.html#hs_a_board + :return: 股票和市场代码 + :rtype: dict + """ + url = "http://80.push2.eastmoney.com/api/qt/clist/get" + params = { + "pn": "1", + "pz": "50000", + "po": "1", + "np": "1", + "ut": "bd1d9ddb04089700cf9c27f6f7426281", + "fltt": "2", + "invt": "2", + "fid": "f3", + "fs": "m:1 t:2,m:1 t:23", + "fields": "f12", + "_": "1623833739532", + } + r = requests.get(url, params=params) + data_json = r.json() + if not data_json["data"]["diff"]: + return dict() + temp_df = pd.DataFrame(data_json["data"]["diff"]) + temp_df["market_id"] = 1 + temp_df.columns = ["sh_code", "sh_id"] + code_id_dict = dict(zip(temp_df["sh_code"], temp_df["sh_id"])) + params = { + "pn": "1", + "pz": "50000", + "po": "1", + "np": "1", + "ut": "bd1d9ddb04089700cf9c27f6f7426281", + "fltt": "2", + "invt": "2", + "fid": "f3", + "fs": "m:0 t:6,m:0 t:80", + "fields": "f12", + "_": "1623833739532", + } + r = requests.get(url, params=params) + data_json = r.json() + if not data_json["data"]["diff"]: + return dict() + temp_df_sz = pd.DataFrame(data_json["data"]["diff"]) + temp_df_sz["sz_id"] = 0 + code_id_dict.update(dict(zip(temp_df_sz["f12"], temp_df_sz["sz_id"]))) + params = { + "pn": "1", + "pz": "50000", + "po": "1", + "np": "1", + "ut": "bd1d9ddb04089700cf9c27f6f7426281", + "fltt": "2", + "invt": "2", + "fid": "f3", + "fs": "m:0 t:81 s:2048", + "fields": "f12", + "_": "1623833739532", + } + r = requests.get(url, params=params) + data_json = r.json() + if not data_json["data"]["diff"]: + return dict() + temp_df_sz = pd.DataFrame(data_json["data"]["diff"]) + temp_df_sz["bj_id"] = 0 + code_id_dict.update(dict(zip(temp_df_sz["f12"], temp_df_sz["bj_id"]))) + return code_id_dict + + +def __event_stream(url, params): + # 使用 stream=True 参数来启用流式请求 + response = requests.get(url, params=params, stream=True) + event_data = "" + + for line in response.iter_lines(): + # 过滤掉保持连接的空行 + if line: + event_data += line.decode() + "\n" + elif event_data: + yield event_data + event_data = "" + + +def stock_intraday_em(symbol: str = "000001") -> pd.DataFrame: + """ + 东财财富-分时数据 + https://quote.eastmoney.com/f1.html?newcode=0.000001 + :param symbol: 股票代码 + :type symbol: str + :return: 分时数据 + :rtype: pandas.DataFrame + """ + code_id_map_em_dict = __code_id_map_em() + url = "https://70.push2.eastmoney.com/api/qt/stock/details/sse" + params = { + "fields1": "f1,f2,f3,f4", + "fields2": "f51,f52,f53,f54,f55", + "mpi": "2000", + "ut": "bd1d9ddb04089700cf9c27f6f7426281", + "fltt": "2", + "pos": "-0", + "secid": f"{code_id_map_em_dict[symbol]}.{symbol}", + "wbp2u": "|0|0|0|web", + } + + big_df = pd.DataFrame() # 创建一个空的 DataFrame + + for event in __event_stream(url, params): + # 从每个事件的数据行中删除 "data: ",然后解析 JSON + event_json = json.loads(event.replace("data: ", "")) + # 将 JSON 数据转换为 DataFrame,然后添加到主 DataFrame 中 + temp_df = pd.DataFrame( + [item.split(",") for item in event_json["data"]["details"]] + ) + big_df = pd.concat([big_df, temp_df], ignore_index=True) + break + + big_df.columns = ["时间", "成交价", "手数", "-", "-"] + big_df = big_df[["时间", "成交价", "手数"]] + big_df["成交价"] = pd.to_numeric(big_df["成交价"], errors="coerce") + big_df["手数"] = pd.to_numeric(big_df["手数"], errors="coerce") + return big_df + + +if __name__ == "__main__": + stock_intraday_em_df = stock_intraday_em(symbol="000001") + print(stock_intraday_em_df) diff --git a/docs/changelog.md b/docs/changelog.md index d17b269a72b..8b9b2b9f778 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -63,6 +63,10 @@ ## 更新说明详情 +1.11.33 add: add stock_intraday_em interface + + 1. 新增 stock_intraday_em 接口 + 1.11.32 add: add stock_margin_ratio_pa interface 1. 新增 stock_margin_ratio_pa 接口 @@ -2960,6 +2964,8 @@ ## 版本更新说明 +1.11.33 add: add stock_intraday_em interface + 1.11.32 add: add stock_margin_ratio_pa interface 1.11.31 fix: fix futures_dce_warehouse_receipt interface diff --git a/docs/data/stock/stock.md b/docs/data/stock/stock.md index 8468c045f03..dce78472b16 100644 --- a/docs/data/stock/stock.md +++ b/docs/data/stock/stock.md @@ -1507,6 +1507,57 @@ print(stock_zh_a_hist_min_em_df) 143 2021-09-03 15:00:00 3104.09 3100.84 ... 45082686.0 0.16 0.01 ``` +##### 日内分时数据 + +接口: stock_intraday_em + +目标地址: https://quote.eastmoney.com/f1.html?newcode=0.000001 + +描述: 东财财富-分时数据 + +限量: 单次返回指定股票最近一个交易日的分时数据, 包含盘前数据 + +输入参数 + +| 名称 | 类型 | 描述 | +|------------|-----|-------------------------------------| +| symbol | str | symbol="000001"; 股票代码 | + +输出参数 + +| 名称 | 类型 | 描述 | +|-----|---------|----| +| 时间 | object | - | +| 成交价 | float64 | - | +| 手数 | int64 | - | + +接口示例 + +```python +import akshare as ak + +stock_intraday_em_df = ak.stock_intraday_em(symbol="000001") +print(stock_intraday_em_df) +``` + +数据示例 + +``` + 时间 成交价 手数 +0 09:15:00 11.08 43 +1 09:15:09 11.10 607 +2 09:15:18 11.09 893 +3 09:15:27 11.09 1009 +4 09:15:36 11.09 1035 + ... ... ... +4549 14:56:51 11.00 12 +4550 14:56:54 11.00 35 +4551 14:56:57 11.00 14 +4552 14:57:00 10.99 112 +4553 15:00:00 11.00 8365 +[4554 rows x 3 columns] +``` + ##### 盘前数据 接口: stock_zh_a_hist_pre_min_em diff --git a/docs/introduction.md b/docs/introduction.md index 2da47e0e012..6c5a8b0c3d0 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -6,7 +6,7 @@ **风险提示:[AKShare](https://github.com/akfamily/akshare) 开源财经数据接口库所采集的数据皆来自公开的数据源,不涉及任何个人隐私数据和非公开数据。 同时本项目提供的数据接口及相关数据仅用于学术研究,任何个人、机构及团体使用本项目的数据接口及相关数据请注意商业风险。** -1. 本文档更新时间:**2023-10-13**; +1. 本文档更新时间:**2023-10-14**; 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) diff --git a/docs/tutorial.md b/docs/tutorial.md index 935ae0f553b..7d5a35c653f 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -1214,6 +1214,8 @@ "index_us_stock_sina" # 新浪财经-美股指数行情 # 融资融券-标的证券名单及保证金比例查询 "stock_margin_ratio_pa" # 融资融券-标的证券名单及保证金比例查询 + # 东财财富-日内分时数据 + "stock_intraday_em" # 东财财富-日内分时数据 ``` ## 案例演示