akshare-proxy-patch 0.4.2__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ Metadata-Version: 2.4
2
+ Name: akshare-proxy-patch
3
+ Version: 0.5.1
4
+ Summary: 针对 AkShare 和 efinance 的底层补丁,自动为东财等接口注入代理认证头,从而避免 `stock_zh_a_spot_em` 等方法报错。
5
+ Home-page: https://github.com/helloyie/akshare-proxy-patch
6
+ Author: cheapproxy.net
7
+ Author-email: cheapproxy <emezxyj1@gmail.com>
8
+ Project-URL: Homepage, https://github.com/helloyie/akshare-proxy-patch
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.6
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: requests
16
+ Requires-Dist: curl_cffi
17
+ Dynamic: author
18
+ Dynamic: home-page
19
+ Dynamic: license-file
20
+ Dynamic: requires-python
21
+
22
+ # AkShare Proxy Patch
23
+
24
+ > 针对 [akshare](https://github.com/akfamily/akshare)、[efinance](https://github.com/Micro-sheep/efinance)、[yfinance](https://github.com/ranaroussi/yfinance)、[aktools](https://github.com/akfamily/aktools)、[daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis/) 的🐒插件补丁,解决 `stock_zh_a_spot_em`、`stock_zh_a_hist`、`get_realtime_quotes` 等东财接口报错问题和 Yahoo `YFRateLimitError` 问题。并对 `akshare` 部分接口进行多线程加速处理。
25
+
26
+ ## ✨ 特性
27
+
28
+ - 解决 `akshare` 或 `aktools` 的 `stock_zh_a_spot_em`、`stock_zh_a_hist` 接口报错问题
29
+ - 解决 `efinance` 的 `get_realtime_quotes` 等接口报错问题
30
+ - 解决 `yfinance` Yahoo接口国内无法使用问题
31
+ - 解决 `daily_stock_analysis` 的数据获取报错问题
32
+ - 多线程加速 `stock_zh_a_spot_em` 等 `akshare` 接口
33
+
34
+ ## 📦 安装
35
+
36
+ 1. 安装并升级官方 [akshare](https://github.com/akfamily/akshare) 或 [efinance](https://github.com/Micro-sheep/efinance) 包
37
+
38
+ 2. 安装 `akshare-proxy-patch` 插件
39
+
40
+ ```
41
+ pip install akshare-proxy-patch==0.5.1
42
+ ```
43
+
44
+ ## 🚀 使用方法(akshare / efinance / yfinance)
45
+
46
+ 1. [点击前往插件官网](https://ak.cheapproxy.net/dashboard/akshare) 获取 `TOKEN`。
47
+
48
+ 2. `akshare` 或 `efinance` 用户:在 **Python 文件最顶部** 添加如下代码,并替换 `你的TOKEN`。
49
+
50
+ ```
51
+ # python 文件顶部添加如下代码
52
+ # 重要:插件引入和调用一定要放到最顶部!不能在 akshare 或 efinance 之后引入!
53
+
54
+ import akshare_proxy_patch
55
+
56
+ akshare_proxy_patch.install_patch(
57
+ "101.201.173.125",
58
+ auth_token="你的TOKEN",
59
+ retry=30,
60
+ # 封控的域名列表,可自行调整
61
+ hook_domains=[
62
+ "fund.eastmoney.com",
63
+ "push2.eastmoney.com",
64
+ "push2his.eastmoney.com",
65
+ "emweb.securities.eastmoney.com",
66
+ "searchapi.eastmoney.com/api/suggest/get",
67
+ "push2delay.eastmoney.com/api/qt/clist/get"
68
+ ],
69
+ fast=True
70
+ )
71
+
72
+ # --------------------------
73
+ # 后续你的业务代码保持不变
74
+ # --------------------------
75
+
76
+ # 假如你使用 akshare
77
+ import akshare as ak
78
+ df = ak.stock_zh_a_spot_em()
79
+
80
+ # 假如你使用 efinance
81
+ import efinance as ef
82
+ ef.stock.get_realtime_quotes()
83
+ ```
84
+
85
+ 3. `yfinance` 用户:在 Python 文件顶部添加如下代码,并替换 `你的TOKEN`,不需要使用 AI 魔改。
86
+
87
+ ```
88
+ # python 文件顶部添加如下代码
89
+ # 重要:插件引入和调用一定要放到最顶部!不能在 akshare 或 efinance 之后引入!
90
+
91
+ import akshare_proxy_patch
92
+
93
+ akshare_proxy_patch.install_yfinance_patch(
94
+ "101.201.173.125",
95
+ auth_token="你的TOKEN",
96
+ retry=30,
97
+ )
98
+
99
+ # --------------------------
100
+ # 后续你的业务代码保持不变
101
+ # --------------------------
102
+
103
+ import yfinance as yf
104
+
105
+ data = yf.download("AAPL", start="2017-01-01", end="2017-04-30")
106
+ ```
107
+
108
+ 4. 尽量用 [efinance](https://github.com/Micro-sheep/efinance) 替代 akshare 的部分函数配合插件获取数据,效率更高,更省积分。
109
+
110
+ ## install_patch 参数说明
111
+
112
+ - `ip`
113
+ - 网关,不可修改
114
+ - `auth_token`
115
+ - TOKEN 授权凭证
116
+ - `retry`
117
+ - 重试次数,默认为30,建议保持不变
118
+ - `hook_domains`
119
+ - 封控的域名列表,插件会 hook 住这些域名的请求,解除封控。已默认涵盖常见函数的域名。
120
+ - 可点击 `akshare` 或 `efinance` 函数查看接口源码对应的 `URL` 并添加到该列表。
121
+ - 如只用到 `stock_zh_a_spot_em` 这个接口,`hook_domains` 可设置为 `["https://82.push2.eastmoney.com/api/qt/clist/get"]`。
122
+ - `fast`
123
+ - 是否启用 `akshare` 多线程加速,默认开启,加速函数列表如下
124
+ - 所有用到 `fetch_paginated_data` 分页函数的接口,如 `stock_zh_a_spot_em`、`stock_sh_a_spot_em`、`stock_board_industry_cons_em` 等
125
+
126
+ ## 如何在 aktools 内集成插件?
127
+
128
+ - `aktools` 想要集成插件,需下载 [akt.py](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/aktools/akt.py) 文件,并填入您的 `TOKEN`。
129
+ - 然后执行 `python akt.py` 即可启动一个 `http://127.0.0.1:8080/` 服务。只是启动方式不同而已,使用请参考 [aktools 官方文档](https://github.com/akfamily/aktools)。
130
+
131
+ ## 如何在 daily_stock_analysis 内集成插件?
132
+
133
+ - [daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis) 本身集成了 `akshare`、`efinance` 等数据源,如遇数据源报错,可以通过引入插件来解决。
134
+ - 在 [daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis) 项目中,找到入口 Python 文件 `main.py`,在文件顶部引入插件:
135
+
136
+ ```
137
+ # 找到 daily_stock_analysis main.py 的这行(第一行)代码,在其下方引入插件
138
+ from __future__ import annotations
139
+
140
+ # 引入插件
141
+ import akshare_proxy_patch
142
+
143
+ akshare_proxy_patch.install_patch(
144
+ "101.201.173.125",
145
+ auth_token="你的TOKEN",
146
+ retry=30,
147
+ # 封控的域名列表,可自行调整
148
+ hook_domains=[
149
+ "fund.eastmoney.com",
150
+ "push2.eastmoney.com",
151
+ "push2his.eastmoney.com",
152
+ "emweb.securities.eastmoney.com",
153
+ "searchapi.eastmoney.com/api/suggest/get",
154
+ "push2delay.eastmoney.com/api/qt/clist/get"
155
+ ],
156
+ fast=True
157
+ )
158
+ # daily_stock_analysis main.py 的其他后续代码
159
+ # 插件引入成功后正常启动该项目即可
160
+
161
+ ```
162
+
163
+ ## 股票数量很多,如何快速拉取数据?
164
+
165
+ - 尽量使用 `efinance` 替代 `akshare` 来获取数据, `efinance` 内置多线程,效率更高,更省积分。
166
+ - 在大规模快速获取数据时,可使用多进程提升效率。 可以参考 [多进程获取所有股票的历史K线](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/all_quote_history/main.py),修改 `auth_token`,执行 `python main.py` 测试。
167
+
168
+ ## 如何灵活禁用/启用插件?
169
+
170
+ 加入如下代码后插件就会被禁用,[点击查看禁用/启用插件示例](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/disable/disable.py):
171
+
172
+ ```
173
+ akshare_proxy_patch.uninstall_patch()
174
+ ```
175
+
176
+ ## 我没使用 akshare 或 efinance,能集成插件吗?
177
+
178
+ - 如果使用 Python 语言的 `requests` 库请求接口,插件能自动 hook 住请求,正常工作。
179
+ - 如果您使用其他语言或 python 的其他库,可 [手动提取代理](http://101.201.173.125:47001/api/akshare-auth?token=XXX&version=0.5.1) 自行实现封控解除。
180
+
181
+ ## 使用问题交流群
182
+
183
+ 如使用时遇到问题,或对插件有什么意见或建议,可进群交流:
184
+
185
+ ![eozuhg3eH48ntnAnZXjKpmiegT7Ws0cl.webp](https://cdn.nodeimage.com/i/eozuhg3eH48ntnAnZXjKpmiegT7Ws0cl.webp)
@@ -0,0 +1,164 @@
1
+ # AkShare Proxy Patch
2
+
3
+ > 针对 [akshare](https://github.com/akfamily/akshare)、[efinance](https://github.com/Micro-sheep/efinance)、[yfinance](https://github.com/ranaroussi/yfinance)、[aktools](https://github.com/akfamily/aktools)、[daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis/) 的🐒插件补丁,解决 `stock_zh_a_spot_em`、`stock_zh_a_hist`、`get_realtime_quotes` 等东财接口报错问题和 Yahoo `YFRateLimitError` 问题。并对 `akshare` 部分接口进行多线程加速处理。
4
+
5
+ ## ✨ 特性
6
+
7
+ - 解决 `akshare` 或 `aktools` 的 `stock_zh_a_spot_em`、`stock_zh_a_hist` 接口报错问题
8
+ - 解决 `efinance` 的 `get_realtime_quotes` 等接口报错问题
9
+ - 解决 `yfinance` Yahoo接口国内无法使用问题
10
+ - 解决 `daily_stock_analysis` 的数据获取报错问题
11
+ - 多线程加速 `stock_zh_a_spot_em` 等 `akshare` 接口
12
+
13
+ ## 📦 安装
14
+
15
+ 1. 安装并升级官方 [akshare](https://github.com/akfamily/akshare) 或 [efinance](https://github.com/Micro-sheep/efinance) 包
16
+
17
+ 2. 安装 `akshare-proxy-patch` 插件
18
+
19
+ ```
20
+ pip install akshare-proxy-patch==0.5.1
21
+ ```
22
+
23
+ ## 🚀 使用方法(akshare / efinance / yfinance)
24
+
25
+ 1. [点击前往插件官网](https://ak.cheapproxy.net/dashboard/akshare) 获取 `TOKEN`。
26
+
27
+ 2. `akshare` 或 `efinance` 用户:在 **Python 文件最顶部** 添加如下代码,并替换 `你的TOKEN`。
28
+
29
+ ```
30
+ # python 文件顶部添加如下代码
31
+ # 重要:插件引入和调用一定要放到最顶部!不能在 akshare 或 efinance 之后引入!
32
+
33
+ import akshare_proxy_patch
34
+
35
+ akshare_proxy_patch.install_patch(
36
+ "101.201.173.125",
37
+ auth_token="你的TOKEN",
38
+ retry=30,
39
+ # 封控的域名列表,可自行调整
40
+ hook_domains=[
41
+ "fund.eastmoney.com",
42
+ "push2.eastmoney.com",
43
+ "push2his.eastmoney.com",
44
+ "emweb.securities.eastmoney.com",
45
+ "searchapi.eastmoney.com/api/suggest/get",
46
+ "push2delay.eastmoney.com/api/qt/clist/get"
47
+ ],
48
+ fast=True
49
+ )
50
+
51
+ # --------------------------
52
+ # 后续你的业务代码保持不变
53
+ # --------------------------
54
+
55
+ # 假如你使用 akshare
56
+ import akshare as ak
57
+ df = ak.stock_zh_a_spot_em()
58
+
59
+ # 假如你使用 efinance
60
+ import efinance as ef
61
+ ef.stock.get_realtime_quotes()
62
+ ```
63
+
64
+ 3. `yfinance` 用户:在 Python 文件顶部添加如下代码,并替换 `你的TOKEN`,不需要使用 AI 魔改。
65
+
66
+ ```
67
+ # python 文件顶部添加如下代码
68
+ # 重要:插件引入和调用一定要放到最顶部!不能在 akshare 或 efinance 之后引入!
69
+
70
+ import akshare_proxy_patch
71
+
72
+ akshare_proxy_patch.install_yfinance_patch(
73
+ "101.201.173.125",
74
+ auth_token="你的TOKEN",
75
+ retry=30,
76
+ )
77
+
78
+ # --------------------------
79
+ # 后续你的业务代码保持不变
80
+ # --------------------------
81
+
82
+ import yfinance as yf
83
+
84
+ data = yf.download("AAPL", start="2017-01-01", end="2017-04-30")
85
+ ```
86
+
87
+ 4. 尽量用 [efinance](https://github.com/Micro-sheep/efinance) 替代 akshare 的部分函数配合插件获取数据,效率更高,更省积分。
88
+
89
+ ## install_patch 参数说明
90
+
91
+ - `ip`
92
+ - 网关,不可修改
93
+ - `auth_token`
94
+ - TOKEN 授权凭证
95
+ - `retry`
96
+ - 重试次数,默认为30,建议保持不变
97
+ - `hook_domains`
98
+ - 封控的域名列表,插件会 hook 住这些域名的请求,解除封控。已默认涵盖常见函数的域名。
99
+ - 可点击 `akshare` 或 `efinance` 函数查看接口源码对应的 `URL` 并添加到该列表。
100
+ - 如只用到 `stock_zh_a_spot_em` 这个接口,`hook_domains` 可设置为 `["https://82.push2.eastmoney.com/api/qt/clist/get"]`。
101
+ - `fast`
102
+ - 是否启用 `akshare` 多线程加速,默认开启,加速函数列表如下
103
+ - 所有用到 `fetch_paginated_data` 分页函数的接口,如 `stock_zh_a_spot_em`、`stock_sh_a_spot_em`、`stock_board_industry_cons_em` 等
104
+
105
+ ## 如何在 aktools 内集成插件?
106
+
107
+ - `aktools` 想要集成插件,需下载 [akt.py](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/aktools/akt.py) 文件,并填入您的 `TOKEN`。
108
+ - 然后执行 `python akt.py` 即可启动一个 `http://127.0.0.1:8080/` 服务。只是启动方式不同而已,使用请参考 [aktools 官方文档](https://github.com/akfamily/aktools)。
109
+
110
+ ## 如何在 daily_stock_analysis 内集成插件?
111
+
112
+ - [daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis) 本身集成了 `akshare`、`efinance` 等数据源,如遇数据源报错,可以通过引入插件来解决。
113
+ - 在 [daily_stock_analysis](https://github.com/ZhuLinsen/daily_stock_analysis) 项目中,找到入口 Python 文件 `main.py`,在文件顶部引入插件:
114
+
115
+ ```
116
+ # 找到 daily_stock_analysis main.py 的这行(第一行)代码,在其下方引入插件
117
+ from __future__ import annotations
118
+
119
+ # 引入插件
120
+ import akshare_proxy_patch
121
+
122
+ akshare_proxy_patch.install_patch(
123
+ "101.201.173.125",
124
+ auth_token="你的TOKEN",
125
+ retry=30,
126
+ # 封控的域名列表,可自行调整
127
+ hook_domains=[
128
+ "fund.eastmoney.com",
129
+ "push2.eastmoney.com",
130
+ "push2his.eastmoney.com",
131
+ "emweb.securities.eastmoney.com",
132
+ "searchapi.eastmoney.com/api/suggest/get",
133
+ "push2delay.eastmoney.com/api/qt/clist/get"
134
+ ],
135
+ fast=True
136
+ )
137
+ # daily_stock_analysis main.py 的其他后续代码
138
+ # 插件引入成功后正常启动该项目即可
139
+
140
+ ```
141
+
142
+ ## 股票数量很多,如何快速拉取数据?
143
+
144
+ - 尽量使用 `efinance` 替代 `akshare` 来获取数据, `efinance` 内置多线程,效率更高,更省积分。
145
+ - 在大规模快速获取数据时,可使用多进程提升效率。 可以参考 [多进程获取所有股票的历史K线](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/all_quote_history/main.py),修改 `auth_token`,执行 `python main.py` 测试。
146
+
147
+ ## 如何灵活禁用/启用插件?
148
+
149
+ 加入如下代码后插件就会被禁用,[点击查看禁用/启用插件示例](https://github.com/HelloYie/akshare-proxy-patch/blob/master/examples/disable/disable.py):
150
+
151
+ ```
152
+ akshare_proxy_patch.uninstall_patch()
153
+ ```
154
+
155
+ ## 我没使用 akshare 或 efinance,能集成插件吗?
156
+
157
+ - 如果使用 Python 语言的 `requests` 库请求接口,插件能自动 hook 住请求,正常工作。
158
+ - 如果您使用其他语言或 python 的其他库,可 [手动提取代理](http://101.201.173.125:47001/api/akshare-auth?token=XXX&version=0.5.1) 自行实现封控解除。
159
+
160
+ ## 使用问题交流群
161
+
162
+ 如使用时遇到问题,或对插件有什么意见或建议,可进群交流:
163
+
164
+ ![eozuhg3eH48ntnAnZXjKpmiegT7Ws0cl.webp](https://cdn.nodeimage.com/i/eozuhg3eH48ntnAnZXjKpmiegT7Ws0cl.webp)
@@ -1,7 +1,9 @@
1
+ import importlib
2
+ import sys
1
3
  import time
2
4
  import random
3
5
  import threading
4
- import sys
6
+
5
7
 
6
8
  # 1. 导入必要的库
7
9
  try:
@@ -13,7 +15,7 @@ except ImportError:
13
15
 
14
16
  import requests as std_requests
15
17
 
16
- __version__ = "0.4.2"
18
+ __version__ = "0.5.1"
17
19
 
18
20
  # 授权接口固定使用标准 requests,避免干扰
19
21
  _auth_session = std_requests.Session()
@@ -24,6 +26,8 @@ target_browsers = [
24
26
  if isinstance(b, str) and (b.startswith("chrome") or b.startswith("edge"))
25
27
  ]
26
28
 
29
+ origin_backup = {}
30
+
27
31
 
28
32
  class AuthCache:
29
33
  def __init__(self):
@@ -75,6 +79,7 @@ def install_patch(
75
79
  hook_domains=default_hook_domains,
76
80
  timeout=5,
77
81
  cookie="",
82
+ fast=True,
78
83
  ):
79
84
  # --- 核心改进:备份原始 Session 避开递归死循环 ---
80
85
  if not hasattr(std_requests, "_OriginalSession"):
@@ -160,6 +165,32 @@ def install_patch(
160
165
  curl_requests.get = patched_get
161
166
  curl_requests.post = patched_post
162
167
 
168
+ if fast:
169
+ install_fast_patch()
170
+
171
+
172
+ def install_fast_patch():
173
+ """
174
+ 一些函数使用多线程替代分页请求,提升效率
175
+ """
176
+ try:
177
+ akshare = importlib.import_module("akshare")
178
+ try:
179
+ from .akshare_src.func_new import fetch_paginated_data as _patched_fetch_paginated_data
180
+ except:
181
+ from .akshare_src.func import fetch_paginated_data as _patched_fetch_paginated_data
182
+
183
+ # 覆盖已加载的 akshare 模块的 fetch_paginated_data
184
+ for name, mod in list(sys.modules.items()):
185
+ if mod and name.startswith("akshare"):
186
+ if hasattr(mod, "fetch_paginated_data"):
187
+ origin_backup[(name, "fetch_paginated_data")] = getattr(
188
+ mod, "fetch_paginated_data"
189
+ )
190
+ setattr(mod, "fetch_paginated_data", _patched_fetch_paginated_data)
191
+ except Exception as e:
192
+ print("启动快速模式失败:", e)
193
+
163
194
 
164
195
  def install_yfinance_patch(
165
196
  auth_ip, auth_token="", retry=30, hook_domains=["finance.yahoo.com"]
@@ -167,3 +198,20 @@ def install_yfinance_patch(
167
198
  from .yfinance import install_yfinance_patch_main
168
199
 
169
200
  install_yfinance_patch_main(auth_ip, auth_token, retry, hook_domains)
201
+
202
+
203
+ def uninstall_patch():
204
+ """
205
+ 卸载插件,恢复原始状态
206
+ """
207
+ import requests
208
+
209
+ # 恢复 requests 模块到原始状态
210
+ importlib.reload(requests)
211
+ try:
212
+ # 还原 akshare 模块
213
+ for (mod_name, func_name), original_func in origin_backup.items():
214
+ if mod_name in sys.modules:
215
+ setattr(sys.modules[mod_name], func_name, original_func)
216
+ except Exception as e:
217
+ print("还原 akshare 模块失败:", e)
@@ -0,0 +1,90 @@
1
+ # !/usr/bin/env python
2
+ """
3
+ Date: 2025/3/10 18:00
4
+ Desc: 通用帮助函数
5
+ """
6
+
7
+ import math
8
+ import random
9
+ import time
10
+ from concurrent.futures import ThreadPoolExecutor, as_completed
11
+ from typing import List, Dict
12
+
13
+ import pandas as pd
14
+
15
+ from akshare.utils.request import request_with_retry
16
+ from akshare.utils.tqdm import get_tqdm
17
+
18
+
19
+ def fetch_paginated_data(url: str, base_params: Dict, timeout: int = 15):
20
+ """
21
+ 东方财富-分页获取数据并合并结果
22
+ https://quote.eastmoney.com/f1.html?newcode=0.000001
23
+ :param url: 股票代码
24
+ :type url: str
25
+ :param base_params: 基础请求参数
26
+ :type base_params: dict
27
+ :param timeout: 请求超时时间
28
+ :type timeout: str
29
+ :return: 合并后的数据
30
+ :rtype: pandas.DataFrame
31
+ """
32
+ # 复制参数以避免修改原始参数
33
+ params = base_params.copy()
34
+ # 获取第一页数据,用于确定分页信息
35
+ r = request_with_retry(url, params=params, timeout=timeout)
36
+ data_json = r.json()
37
+ # 计算分页信息
38
+ per_page_num = len(data_json["data"]["diff"])
39
+ total_page = math.ceil(data_json["data"]["total"] / per_page_num)
40
+ # 存储所有页面数据
41
+ temp_list = []
42
+ # 添加第一页数据
43
+ temp_list.append(pd.DataFrame(data_json["data"]["diff"]))
44
+ # 获取剩余页面数据
45
+ remaining_pages = list(range(2, total_page + 1))
46
+ if remaining_pages:
47
+ tqdm = get_tqdm()
48
+
49
+ def fetch_page(page: int) -> pd.DataFrame:
50
+ current_params = base_params.copy()
51
+ current_params.update({"pn": page})
52
+ time.sleep(random.uniform(0.5, 1.5))
53
+ r = request_with_retry(url, params=current_params, timeout=timeout)
54
+ page_json = r.json()
55
+ return pd.DataFrame(page_json["data"]["diff"])
56
+
57
+ with ThreadPoolExecutor(max_workers=min(8, len(remaining_pages))) as executor:
58
+ future_to_page = {
59
+ executor.submit(fetch_page, page): page for page in remaining_pages
60
+ }
61
+ for future in tqdm(
62
+ as_completed(future_to_page), total=len(remaining_pages), leave=False
63
+ ):
64
+ inner_temp_df = future.result()
65
+ temp_list.append(inner_temp_df)
66
+
67
+ # 合并所有数据
68
+ temp_df = pd.concat(temp_list, ignore_index=True)
69
+ temp_df["f3"] = pd.to_numeric(temp_df["f3"], errors="coerce")
70
+ temp_df.sort_values(by=["f3"], ascending=False, inplace=True, ignore_index=True)
71
+ temp_df.reset_index(inplace=True)
72
+ temp_df["index"] = temp_df["index"].astype(int) + 1
73
+ return temp_df
74
+
75
+
76
+ def set_df_columns(df: pd.DataFrame, cols: List[str]) -> pd.DataFrame:
77
+ """
78
+ 设置 pandas.DataFrame 为空的情况
79
+ :param df: 需要设置命名的数据框
80
+ :type df: pandas.DataFrame
81
+ :param cols: 字段的列表
82
+ :type cols: list
83
+ :return: 重新设置后的数据
84
+ :rtype: pandas.DataFrame
85
+ """
86
+ if df.shape == (0, 0):
87
+ return pd.DataFrame(data=[], columns=cols)
88
+ else:
89
+ df.columns = cols
90
+ return df
@@ -0,0 +1,97 @@
1
+ # !/usr/bin/env python
2
+ """
3
+ Date: 2025/3/10 18:00
4
+ Desc: 通用帮助函数
5
+ """
6
+
7
+ import math
8
+ import random
9
+ import time
10
+ from concurrent.futures import ThreadPoolExecutor, as_completed
11
+ from typing import List, Dict
12
+
13
+ import pandas as pd
14
+
15
+ from akshare.utils import http
16
+ from akshare.utils.tqdm import get_tqdm
17
+
18
+
19
+ def _get_page(url: str, params: Dict):
20
+ """
21
+ 取一页东方财富分页数据;每次新建连接,失败时按 http.request_with_retry 的策略重试
22
+ :return: 响应对象
23
+ :rtype: requests.Response
24
+ """
25
+ r = http.request_with_retry("get", url, params=params, fresh_session=True)
26
+ r.raise_for_status()
27
+ return r
28
+
29
+
30
+ def fetch_paginated_data(url: str, base_params: Dict):
31
+ """
32
+ 东方财富-分页获取数据并合并结果
33
+ https://quote.eastmoney.com/f1.html?newcode=0.000001
34
+ :param url: 股票代码
35
+ :type url: str
36
+ :param base_params: 基础请求参数
37
+ :type base_params: dict
38
+ :return: 合并后的数据
39
+ :rtype: pandas.DataFrame
40
+ """
41
+ # 复制参数以避免修改原始参数
42
+ params = base_params.copy()
43
+ # 获取第一页数据,用于确定分页信息
44
+ r = _get_page(url, params)
45
+ data_json = r.json()
46
+ # 计算分页信息
47
+ per_page_num = len(data_json["data"]["diff"])
48
+ total_page = math.ceil(data_json["data"]["total"] / per_page_num)
49
+ # 存储所有页面数据
50
+ temp_list = []
51
+ # 添加第一页数据
52
+ temp_list.append(pd.DataFrame(data_json["data"]["diff"]))
53
+ # 获取进度条
54
+ tqdm = get_tqdm()
55
+ # 并发获取剩余页面;每个任务使用独立参数,避免线程间共享可变状态。
56
+ def fetch_page(page: int):
57
+ page_params = base_params.copy()
58
+ page_params.update({"pn": page})
59
+ # 保留原有随机延迟策略,降低请求过于频繁的概率。
60
+ time.sleep(random.uniform(0.5, 1.5))
61
+ r = _get_page(url, page_params)
62
+ return pd.DataFrame(r.json()["data"]["diff"])
63
+
64
+ pages = range(2, total_page + 1)
65
+ if pages:
66
+ # 固定并发上限,避免页数较多时创建过多线程。
67
+ with ThreadPoolExecutor(max_workers=min(8, len(pages))) as executor:
68
+ futures = {executor.submit(fetch_page, page): page for page in pages}
69
+ page_results = {}
70
+ for future in tqdm(as_completed(futures), total=len(futures), leave=False):
71
+ page_results[futures[future]] = future.result()
72
+ # 按页码恢复原顺序,保持合并及相同排序键下的数据顺序不受并发完成顺序影响。
73
+ temp_list.extend(page_results[page] for page in pages)
74
+ # 合并所有数据
75
+ temp_df = pd.concat(temp_list, ignore_index=True)
76
+ temp_df["f3"] = pd.to_numeric(temp_df["f3"], errors="coerce")
77
+ temp_df = temp_df.sort_values(by=["f3"], ascending=False, ignore_index=True)
78
+ temp_df = temp_df.reset_index()
79
+ temp_df["index"] = temp_df["index"].astype(int) + 1
80
+ return temp_df
81
+
82
+
83
+ def set_df_columns(df: pd.DataFrame, cols: List[str]) -> pd.DataFrame:
84
+ """
85
+ 设置 pandas.DataFrame 为空的情况
86
+ :param df: 需要设置命名的数据框
87
+ :type df: pandas.DataFrame
88
+ :param cols: 字段的列表
89
+ :type cols: list
90
+ :return: 重新设置后的数据
91
+ :rtype: pandas.DataFrame
92
+ """
93
+ if df.shape == (0, 0):
94
+ return pd.DataFrame(data=[], columns=cols)
95
+ else:
96
+ df.columns = cols
97
+ return df