mns-common 1.5.2.5__py3-none-any.whl → 1.5.2.6__py3-none-any.whl

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.

Potentially problematic release.


This version of mns-common might be problematic. Click here for more details.

@@ -7,11 +7,21 @@ project_path = file_path[0:end]
7
7
  sys.path.append(project_path)
8
8
 
9
9
  import requests
10
+
11
+ import mns_common.utils.data_frame_util as data_frame_util
10
12
  import json
11
- import pandas as pd
12
- from concurrent.futures import ThreadPoolExecutor
13
13
  import datetime
14
+
15
+ import threading
16
+ from concurrent.futures import ThreadPoolExecutor
17
+ import mns_common.component.proxies.proxy_common_api as proxy_common_api
14
18
  from loguru import logger
19
+ import concurrent.futures
20
+ import pandas as pd
21
+ import time
22
+ from concurrent.futures import ThreadPoolExecutor, as_completed
23
+ from threading import Lock
24
+ import mns_common.api.em.real_time.real_time_quotes_repeat_api as real_time_quotes_repeat_api
15
25
 
16
26
  #
17
27
  # fields_02 = "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,f25,f26,f27,f28,f29,f30,f31,f32,f33,f34,f35,f36,f37,f38,f39,f40,f41,f42,f43,f44,f45,f46,f47,f48,f49,f50,f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65,f66,f67,f68,f69,f70,f71,f72,f73,f74,f75,f76,f77,f78,f79,f80,f81,f82,f83,f84,f85,f86,f87,f88,f89,f90,f91,f92,f93,f94,f95,f96,f97,f98,f99,f100,f101,f102,f103,f104,f105,f106,f107,f108" \
@@ -20,6 +30,8 @@ from loguru import logger
20
30
  # ",f309,f310,f312,f313,f314,f315,f316,f317,f318,f319,f320,f321,f322,f323,f324,f325,f326,f327,f328,f329,f330,f331,f332,f333,f334,f335,f336,f337,f338,f339,f340,f341,f342,f343,f344,f345,f346,f347,f348,f349,f350,f351,f352,f353,f354,f355,f356,f357,f358,f359,f360,f361,f362,f363,f364,f365,f366,f367,f368,f369,f370,f371,f372,f373,f374,f375,f376,f377,f378,f379,f380,f381,f382,f383,f384,f385,f386,f387,f388,f389,f390,f391,f392,f393,f394,f395,f396,f397,f398,f399,f401"
21
31
 
22
32
 
33
+ fs = "m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048"
34
+
23
35
  fields = ("f2,f3,f5,f6,f8,"
24
36
  "f9,f10,f22,f12,f13,"
25
37
  "f14,f15,f16,f17,f18,"
@@ -39,7 +51,7 @@ min_number = 5600
39
51
  page_number = 100
40
52
 
41
53
 
42
- def get_stock_page_data(pn, fields, fs, proxies):
54
+ def get_stock_page_data(pn, proxies, page_number, time_out):
43
55
  """
44
56
  获取单页股票数据
45
57
  """
@@ -53,7 +65,7 @@ def get_stock_page_data(pn, fields, fs, proxies):
53
65
  params = {
54
66
  "cb": "jQuery1124046660442520420653_" + str(current_timestamp_ms),
55
67
  "pn": str(pn),
56
- "pz": "10000", # 每页最大200条
68
+ "pz": str(page_number), # 每页最大200条
57
69
  "po": "1",
58
70
  "np": "3",
59
71
  "ut": "bd1d9ddb04089700cf9c27f6f7426281",
@@ -67,9 +79,9 @@ def get_stock_page_data(pn, fields, fs, proxies):
67
79
  }
68
80
  try:
69
81
  if proxies is None:
70
- r = requests.get(url, params)
82
+ r = requests.get(url, params, timeout=time_out)
71
83
  else:
72
- r = requests.get(url, params, proxies=proxies)
84
+ r = requests.get(url, params, proxies=proxies, timeout=time_out)
73
85
  data_text = r.text
74
86
  begin_index = data_text.index('[')
75
87
  end_index = data_text.index(']')
@@ -113,7 +125,6 @@ def all_stock_ticker_data_new(fields, fs, proxies) -> pd.DataFrame:
113
125
 
114
126
 
115
127
  def get_all_real_time_quotes(proxies):
116
- fs = "m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048"
117
128
  # 获取第一页数据
118
129
  page_one_df = get_stock_page_data(1, fields, fs, proxies)
119
130
  # 数据接口正常返回5600以上的数量
@@ -286,6 +297,91 @@ def rename_real_time_quotes_df(temp_df):
286
297
  return temp_df
287
298
 
288
299
 
300
+ def repeated_acquisition_ask_async(time_out, max_number, num_threads, pages_per_thread):
301
+ per_page = page_number
302
+ total_pages = (max_number + per_page - 1) // per_page # 向上取整
303
+ result_df = pd.DataFrame()
304
+
305
+ # 创建线程锁以确保线程安全
306
+ df_lock = Lock()
307
+
308
+ # 计算每个线程处理的页数范围
309
+ def process_page_range(start_page, end_page, thread_id):
310
+ nonlocal result_df
311
+ local_df = pd.DataFrame()
312
+ current_page = start_page
313
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
314
+
315
+ while current_page <= end_page and current_page <= total_pages:
316
+ proxies = {"https": proxy_ip, "http": proxy_ip}
317
+ try:
318
+ page_df = get_stock_page_data(current_page, proxies, page_number, time_out)
319
+ if data_frame_util.is_not_empty(page_df):
320
+ local_df = pd.concat([local_df, page_df])
321
+ logger.info("线程{}获取页面数据成功: {}", thread_id, current_page)
322
+ current_page += 1
323
+ else:
324
+ time.sleep(0.2)
325
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
326
+ logger.info("线程{}获取页面数据失败: {}", thread_id, current_page)
327
+ except BaseException as e:
328
+ time.sleep(1)
329
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
330
+ logger.error("线程{}处理页面{}时发生错误: {}", thread_id, current_page, e)
331
+
332
+ with df_lock:
333
+ result_df = pd.concat([result_df, local_df])
334
+ return len(local_df)
335
+
336
+ # 计算每个线程的页面范围
337
+ page_ranges = []
338
+ for i in range(num_threads):
339
+ start_page = i * pages_per_thread + 1
340
+ end_page = (i + 1) * pages_per_thread
341
+ if start_page > total_pages:
342
+ break
343
+ page_ranges.append((start_page, end_page, i + 1))
344
+
345
+ # 使用线程池执行任务
346
+ with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
347
+ # 提交所有任务
348
+ futures = [
349
+ executor.submit(process_page_range, start, end, tid)
350
+ for start, end, tid in page_ranges
351
+ ]
352
+
353
+ # 等待所有任务完成并获取结果
354
+ results = []
355
+ for future in concurrent.futures.as_completed(futures):
356
+ try:
357
+ result = future.result()
358
+ results.append(result)
359
+ except Exception as e:
360
+ logger.error("线程执行出错: {}", e)
361
+
362
+ return rename_real_time_quotes_df(result_df)
363
+
364
+
365
+ def get_stock_real_time_quotes(time_out, pages_per_thread):
366
+ try_numer = 3
367
+ while try_numer > 0:
368
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
369
+ proxies = {"https": proxy_ip,
370
+ "http": proxy_ip}
371
+
372
+ total_number = real_time_quotes_repeat_api.get_stocks_num(1, proxies, 20, time_out)
373
+ if total_number > 0:
374
+ break
375
+ try_numer = try_numer - 1
376
+ if total_number == 0:
377
+ return pd.DataFrame()
378
+
379
+ total_pages = (max_number + page_number - 1) // page_number # 向上取整
380
+
381
+ num_threads = int((total_pages / pages_per_thread) + 1)
382
+ return repeated_acquisition_ask_async(time_out, max_number, num_threads, pages_per_thread)
383
+
384
+
289
385
  # 示例调用
290
386
  if __name__ == "__main__":
291
387
  number = 1
@@ -230,240 +230,10 @@ def get_hk_real_time_quotes(cookie, proxies):
230
230
  return page_df
231
231
 
232
232
 
233
- # 获取港股通名单 todo 被封以后替换
234
- def stock_hk_ggt_components_em(cookie, pn, proxies, page_size, time_out) -> pd.DataFrame:
235
- """
236
- 东方财富网-行情中心-港股市场-港股通成份股
237
- https://quote.eastmoney.com/center/gridlist.html#hk_components
238
- :return: 港股通成份股
239
- :rtype: pandas.DataFrame
240
- """
241
- headers = {
242
- 'Cookie': cookie
243
- }
244
- url = "https://33.push2.eastmoney.com/api/qt/clist/get"
245
- params = {
246
- "pn": str(pn),
247
- "pz": str(page_size),
248
- "po": "1",
249
- "np": "2",
250
- "ut": "bd1d9ddb04089700cf9c27f6f7426281",
251
- "fltt": "2",
252
- "fid": "f3",
253
- "fs": "b:DLMK0146,b:DLMK0144",
254
- "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,"
255
- "f25,f26,f22,f33,f11,f62,f128,f136,f115,f152",
256
- "_": "1639974456250",
257
- }
258
- try:
259
- if proxies is None:
260
- r = requests.get(url, params=params, timeout=time_out, headers=headers)
261
- else:
262
- r = requests.get(url, params=params, proxies=proxies, timeout=time_out, headers=headers)
263
-
264
- data_json = r.json()
265
- temp_df = pd.DataFrame(data_json["data"]["diff"]).T
266
- temp_df.reset_index(inplace=True)
267
- temp_df["index"] = temp_df.index + 1
268
- return temp_df
269
- except Exception as e:
270
- logger.error("获取港股通列表:{}", e)
271
-
272
-
273
- def rename_hg_ggt(temp_df):
274
- temp_df.columns = [
275
- "序号",
276
- "-",
277
- "最新价",
278
- "涨跌幅",
279
- "涨跌额",
280
- "成交量",
281
- "成交额",
282
- "-",
283
- "-",
284
- "-",
285
- "-",
286
- "-",
287
- "代码",
288
- "-",
289
- "名称",
290
- "最高",
291
- "最低",
292
- "今开",
293
- "昨收",
294
- "-",
295
- "-",
296
- "-",
297
- "-",
298
- "-",
299
- "-",
300
- "-",
301
- "-",
302
- "-",
303
- "-",
304
- "-",
305
- "-",
306
- "-",
307
- "-",
308
- "-",
309
- "-",
310
- ]
311
- temp_df = temp_df[
312
- [
313
- "序号",
314
- "代码",
315
- "名称",
316
- "最新价",
317
- "涨跌额",
318
- "涨跌幅",
319
- "今开",
320
- "最高",
321
- "最低",
322
- "昨收",
323
- "成交量",
324
- "成交额",
325
- ]
326
- ]
327
-
328
- temp_df = temp_df.rename(columns={
329
- "序号": "index",
330
- "代码": "symbol",
331
- "名称": "name",
332
- "最新价": "now_price",
333
- "涨跌额": "range",
334
- "涨跌幅": "chg",
335
- "今开": "open",
336
- "最高": "high",
337
- "最低": "low",
338
- "昨收": "yesterday_price",
339
- "成交额": "amount",
340
- "成交量": "volume",
341
- })
342
-
343
- return temp_df
344
-
345
-
346
- def get_stock_hk_ggt_components_em_count(cookie, pn, proxies, page_size, time_out):
347
- headers = {
348
- 'Cookie': cookie
349
- }
350
- url = "https://33.push2.eastmoney.com/api/qt/clist/get"
351
- current_timestamp = str(int(round(time.time() * 1000, 0)))
352
- params = {
353
- "pn": str(pn),
354
- "pz": str(page_size),
355
- "po": "1",
356
- "np": "2",
357
- "ut": "bd1d9ddb04089700cf9c27f6f7426281",
358
- "fltt": "2",
359
- "fid": "f3",
360
- "fs": "b:DLMK0146,b:DLMK0144",
361
- "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,"
362
- "f25,f26,f22,f33,f11,f62,f128,f136,f115,f152",
363
- "_": str(current_timestamp),
364
- }
365
- try:
366
- if proxies is None:
367
- r = requests.get(url, params, timeout=time_out, headers=headers)
368
- else:
369
- r = requests.get(url, params, proxies=proxies, timeout=time_out, headers=headers)
370
- data_json = r.json()
371
- total_number = int(data_json['data']['total'])
372
- return total_number
373
- except Exception as e:
374
- logger.error("获取港股通列表,实时行情异常:{}", e)
375
- return 0
376
-
377
-
378
- def repeated_acquisition_ask_hk_gtt_async(em_cookie, time_out, max_number, num_threads, pages_per_thread):
379
- per_page = page_number
380
- total_pages = (max_number + per_page - 1) // per_page # 向上取整
381
- result_df = pd.DataFrame()
382
-
383
- # 创建线程锁以确保线程安全
384
- df_lock = Lock()
385
-
386
- # 计算每个线程处理的页数范围
387
- def process_page_range(start_page, end_page, thread_id):
388
- nonlocal result_df
389
- local_df = pd.DataFrame()
390
- current_page = start_page
391
- proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
392
-
393
- while current_page <= end_page and current_page <= total_pages:
394
- proxies = {"https": proxy_ip, "http": proxy_ip}
395
- try:
396
- page_df = stock_hk_ggt_components_em(em_cookie, current_page, proxies, page_number, time_out)
397
- if data_frame_util.is_not_empty(page_df):
398
- local_df = pd.concat([local_df, page_df])
399
- logger.info("线程{}获取页面数据成功: {}", thread_id, current_page)
400
- current_page += 1
401
- else:
402
- time.sleep(0.2)
403
- proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
404
- logger.info("线程{}获取页面数据失败: {}", thread_id, current_page)
405
- except BaseException as e:
406
- time.sleep(1)
407
- proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
408
- logger.error("线程{}处理页面{}时发生错误: {}", thread_id, current_page, e)
409
-
410
- with df_lock:
411
- result_df = pd.concat([result_df, local_df])
412
- return len(local_df)
413
-
414
- # 计算每个线程的页面范围
415
- page_ranges = []
416
- for i in range(num_threads):
417
- start_page = i * pages_per_thread + 1
418
- end_page = (i + 1) * pages_per_thread
419
- if start_page > total_pages:
420
- break
421
- page_ranges.append((start_page, end_page, i + 1))
422
-
423
- # 使用线程池执行任务
424
- with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
425
- # 提交所有任务
426
- futures = [
427
- executor.submit(process_page_range, start, end, tid)
428
- for start, end, tid in page_ranges
429
- ]
430
-
431
- # 等待所有任务完成并获取结果
432
- results = []
433
- for future in concurrent.futures.as_completed(futures):
434
- try:
435
- result = future.result()
436
- results.append(result)
437
- except Exception as e:
438
- logger.error("线程执行出错: {}", e)
439
-
440
- return rename_hg_ggt(result_df)
441
-
442
-
443
- # 港股通
444
-
445
-
446
- def get_ggt_real_time_quotes(em_cookie, time_out, pages_per_thread):
447
- try_numer = 3
448
- while try_numer > 0:
449
- proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
450
- proxies = {"https": proxy_ip,
451
- "http": proxy_ip}
452
-
453
- max_number = get_stock_hk_ggt_components_em_count(em_cookie, 1, proxies, 20, time_out)
454
- if max_number > 0:
455
- break
456
- try_numer = try_numer - 1
457
- if max_number == 0:
458
- return pd.DataFrame()
459
-
460
- total_pages = (max_number + page_number - 1) // page_number # 向上取整
461
233
 
462
- num_threads = int((total_pages / pages_per_thread) + 1)
463
- return repeated_acquisition_ask_hk_gtt_async(em_cookie, time_out, max_number, num_threads, pages_per_thread)
464
234
 
465
235
 
466
236
  if __name__ == '__main__':
467
- em_cookie = cookie_info_service.get_em_cookie()
468
- test_df = get_ggt_real_time_quotes(em_cookie, 30, 6)
237
+ em_cookie_test = cookie_info_service.get_em_cookie()
238
+ test_df = get_ggt_real_time_quotes(em_cookie_test, 30, 6)
469
239
  print(test_df)
@@ -0,0 +1,260 @@
1
+ import sys
2
+ import os
3
+
4
+ file_path = os.path.abspath(__file__)
5
+ end = file_path.index('mns') + 16
6
+ project_path = file_path[0:end]
7
+ sys.path.append(project_path)
8
+
9
+ import pandas as pd
10
+ from loguru import logger
11
+ import requests
12
+ import time
13
+ import mns_common.component.proxies.proxy_common_api as proxy_common_api
14
+ import concurrent.futures
15
+
16
+ from threading import Lock
17
+ import mns_common.utils.data_frame_util as data_frame_util
18
+
19
+ # 分页条数
20
+ page_number = 100
21
+
22
+
23
+ def rename_hg_ggt(temp_df):
24
+ temp_df.columns = [
25
+ "序号",
26
+ "-",
27
+ "最新价",
28
+ "涨跌幅",
29
+ "涨跌额",
30
+ "成交量",
31
+ "成交额",
32
+ "-",
33
+ "-",
34
+ "-",
35
+ "-",
36
+ "-",
37
+ "代码",
38
+ "-",
39
+ "名称",
40
+ "最高",
41
+ "最低",
42
+ "今开",
43
+ "昨收",
44
+ "-",
45
+ "-",
46
+ "-",
47
+ "-",
48
+ "-",
49
+ "-",
50
+ "-",
51
+ "-",
52
+ "-",
53
+ "-",
54
+ "-",
55
+ "-",
56
+ "-",
57
+ "-",
58
+ "-",
59
+ "-",
60
+ ]
61
+ temp_df = temp_df[
62
+ [
63
+ "序号",
64
+ "代码",
65
+ "名称",
66
+ "最新价",
67
+ "涨跌额",
68
+ "涨跌幅",
69
+ "今开",
70
+ "最高",
71
+ "最低",
72
+ "昨收",
73
+ "成交量",
74
+ "成交额",
75
+ ]
76
+ ]
77
+
78
+ temp_df = temp_df.rename(columns={
79
+ "序号": "index",
80
+ "代码": "symbol",
81
+ "名称": "name",
82
+ "最新价": "now_price",
83
+ "涨跌额": "range",
84
+ "涨跌幅": "chg",
85
+ "今开": "open",
86
+ "最高": "high",
87
+ "最低": "low",
88
+ "昨收": "yesterday_price",
89
+ "成交额": "amount",
90
+ "成交量": "volume",
91
+ })
92
+
93
+ return temp_df
94
+
95
+
96
+ # 获取港股通个数
97
+ def get_stock_hk_ggt_components_em_count(cookie, pn, proxies, page_size, time_out):
98
+ headers = {
99
+ 'Cookie': cookie
100
+ }
101
+ url = "https://33.push2.eastmoney.com/api/qt/clist/get"
102
+ current_timestamp = str(int(round(time.time() * 1000, 0)))
103
+ params = {
104
+ "pn": str(pn),
105
+ "pz": str(page_size),
106
+ "po": "1",
107
+ "np": "2",
108
+ "ut": "bd1d9ddb04089700cf9c27f6f7426281",
109
+ "fltt": "2",
110
+ "fid": "f3",
111
+ "fs": "b:DLMK0146,b:DLMK0144",
112
+ "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,"
113
+ "f25,f26,f22,f33,f11,f62,f128,f136,f115,f152",
114
+ "_": str(current_timestamp),
115
+ }
116
+ try:
117
+ if proxies is None:
118
+ r = requests.get(url, params, timeout=time_out, headers=headers)
119
+ else:
120
+ r = requests.get(url, params, proxies=proxies, timeout=time_out, headers=headers)
121
+ data_json = r.json()
122
+ total_number = int(data_json['data']['total'])
123
+ return total_number
124
+ except Exception as e:
125
+ logger.error("获取港股通列表,实时行情异常:{}", e)
126
+ return 0
127
+
128
+
129
+ # 获取港股通名单 todo 被封以后替换
130
+ def stock_hk_ggt_components_em(cookie, pn, proxies, page_size, time_out) -> pd.DataFrame:
131
+ """
132
+ 东方财富网-行情中心-港股市场-港股通成份股
133
+ https://quote.eastmoney.com/center/gridlist.html#hk_components
134
+ :return: 港股通成份股
135
+ :rtype: pandas.DataFrame
136
+ """
137
+ headers = {
138
+ 'Cookie': cookie
139
+ }
140
+ url = "https://33.push2.eastmoney.com/api/qt/clist/get"
141
+ params = {
142
+ "pn": str(pn),
143
+ "pz": str(page_size),
144
+ "po": "1",
145
+ "np": "2",
146
+ "ut": "bd1d9ddb04089700cf9c27f6f7426281",
147
+ "fltt": "2",
148
+ "fid": "f3",
149
+ "fs": "b:DLMK0146,b:DLMK0144",
150
+ "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,"
151
+ "f25,f26,f22,f33,f11,f62,f128,f136,f115,f152",
152
+ "_": "1639974456250",
153
+ }
154
+ try:
155
+ if proxies is None:
156
+ r = requests.get(url, params=params, timeout=time_out, headers=headers)
157
+ else:
158
+ r = requests.get(url, params=params, proxies=proxies, timeout=time_out, headers=headers)
159
+
160
+ data_json = r.json()
161
+ temp_df = pd.DataFrame(data_json["data"]["diff"]).T
162
+ temp_df.reset_index(inplace=True)
163
+ temp_df["index"] = temp_df.index + 1
164
+ return temp_df
165
+ except Exception as e:
166
+ logger.error("获取港股通列表:{}", e)
167
+
168
+
169
+ def repeated_acquisition_ask_hk_gtt_async(em_cookie, time_out, max_number, num_threads, pages_per_thread):
170
+ per_page = page_number
171
+ total_pages = (max_number + per_page - 1) // per_page # 向上取整
172
+ result_df = pd.DataFrame()
173
+
174
+ # 创建线程锁以确保线程安全
175
+ df_lock = Lock()
176
+
177
+ # 计算每个线程处理的页数范围
178
+ def process_page_range(start_page, end_page, thread_id):
179
+ nonlocal result_df
180
+ local_df = pd.DataFrame()
181
+ current_page = start_page
182
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
183
+
184
+ while current_page <= end_page and current_page <= total_pages:
185
+ proxies = {"https": proxy_ip, "http": proxy_ip}
186
+ try:
187
+ page_df = stock_hk_ggt_components_em(em_cookie, current_page, proxies, page_number, time_out)
188
+ if data_frame_util.is_not_empty(page_df):
189
+ local_df = pd.concat([local_df, page_df])
190
+ logger.info("线程{}获取页面数据成功: {}", thread_id, current_page)
191
+ current_page += 1
192
+ else:
193
+ time.sleep(0.2)
194
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
195
+ logger.info("线程{}获取页面数据失败: {}", thread_id, current_page)
196
+ except BaseException as e:
197
+ time.sleep(1)
198
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
199
+ logger.error("线程{}处理页面{}时发生错误: {}", thread_id, current_page, e)
200
+
201
+ with df_lock:
202
+ result_df = pd.concat([result_df, local_df])
203
+ return len(local_df)
204
+
205
+ # 计算每个线程的页面范围
206
+ page_ranges = []
207
+ for i in range(num_threads):
208
+ start_page = i * pages_per_thread + 1
209
+ end_page = (i + 1) * pages_per_thread
210
+ if start_page > total_pages:
211
+ break
212
+ page_ranges.append((start_page, end_page, i + 1))
213
+
214
+ # 使用线程池执行任务
215
+ with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
216
+ # 提交所有任务
217
+ futures = [
218
+ executor.submit(process_page_range, start, end, tid)
219
+ for start, end, tid in page_ranges
220
+ ]
221
+
222
+ # 等待所有任务完成并获取结果
223
+ results = []
224
+ for future in concurrent.futures.as_completed(futures):
225
+ try:
226
+ result = future.result()
227
+ results.append(result)
228
+ except Exception as e:
229
+ logger.error("线程执行出错: {}", e)
230
+
231
+ return rename_hg_ggt(result_df)
232
+
233
+
234
+ # 港股通实时行情
235
+ def get_ggt_real_time_quotes(em_cookie, time_out, pages_per_thread):
236
+ try_numer = 3
237
+ while try_numer > 0:
238
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
239
+ proxies = {"https": proxy_ip,
240
+ "http": proxy_ip}
241
+
242
+ max_number = get_stock_hk_ggt_components_em_count(em_cookie, 1, proxies, 20, time_out)
243
+ if max_number > 0:
244
+ break
245
+ try_numer = try_numer - 1
246
+ if max_number == 0:
247
+ return pd.DataFrame()
248
+
249
+ total_pages = (max_number + page_number - 1) // page_number # 向上取整
250
+
251
+ num_threads = int((total_pages / pages_per_thread) + 1)
252
+ return repeated_acquisition_ask_hk_gtt_async(em_cookie, time_out, max_number, num_threads, pages_per_thread)
253
+
254
+
255
+ import mns_common.component.cookie.cookie_info_service as cookie_info_service
256
+
257
+ if __name__ == '__main__':
258
+ em_cookie_test = cookie_info_service.get_em_cookie()
259
+ test_df = get_ggt_real_time_quotes(em_cookie_test, 30, 6)
260
+ print(test_df)
@@ -22,6 +22,53 @@ fs = "m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048"
22
22
  PAGE_SIZE = 100
23
23
 
24
24
 
25
+ def get_stocks_num(pn, proxies, page_size, time_out):
26
+ """
27
+ 获取单页股票数据
28
+ """
29
+ # 获取当前日期和时间
30
+ current_time = datetime.datetime.now()
31
+
32
+ # 将当前时间转换为时间戳(以毫秒为单位)
33
+ current_timestamp_ms = int(current_time.timestamp() * 1000)
34
+
35
+ url = "https://33.push2.eastmoney.com/api/qt/clist/get"
36
+ params = {
37
+ "cb": "jQuery1124046660442520420653_" + str(current_timestamp_ms),
38
+ "pn": str(pn),
39
+ "pz": str(page_size), # 每页最大200条
40
+ "po": "0",
41
+ "np": "3",
42
+ "ut": "bd1d9ddb04089700cf9c27f6f7426281",
43
+ "fltt": "2",
44
+ "invt": "2",
45
+ "wbp2u": "|0|0|0|web",
46
+ "fid": "f12",
47
+ "fs": fs,
48
+ "fields": fields,
49
+ "_": current_timestamp_ms
50
+ }
51
+ try:
52
+ if proxies is None:
53
+ r = requests.get(url, params, timeout=time_out)
54
+ else:
55
+ r = requests.get(url, params, proxies=proxies, timeout=time_out)
56
+
57
+ data_text = r.text
58
+
59
+ begin_index_total = data_text.index('"total":')
60
+
61
+ end_index_total = data_text.index('"diff"')
62
+
63
+ total_number = int(data_text[begin_index_total + 8:end_index_total - 1])
64
+
65
+ return total_number
66
+
67
+ except Exception as e:
68
+ logger.error("获取第{}页股票列表异常:{}", pn, str(e))
69
+ return 0
70
+
71
+
25
72
  def get_stock_page_data_time_out(pn, proxies, page_size, time_out):
26
73
  """
27
74
  获取单页股票数据
@@ -348,6 +395,26 @@ def rename_real_time_quotes_df(temp_df):
348
395
  return temp_df
349
396
 
350
397
 
398
+ def get_stock_real_time_quotes(time_out, pages_per_thread):
399
+ try_numer = 3
400
+ while try_numer > 0:
401
+ proxy_ip = proxy_common_api.generate_proxy_ip_api(1)
402
+ proxies = {"https": proxy_ip,
403
+ "http": proxy_ip}
404
+
405
+ total_number = get_stocks_num(1, proxies, 20, time_out)
406
+ if total_number > 0:
407
+ break
408
+ try_numer = try_numer - 1
409
+ if total_number == 0:
410
+ return pd.DataFrame()
411
+
412
+ total_pages = (max_number + PAGE_SIZE - 1) // PAGE_SIZE # 向上取整
413
+
414
+ num_threads = int((total_pages / pages_per_thread) + 1)
415
+ return repeated_acquisition_ask_async(time_out, max_number, num_threads, pages_per_thread)
416
+
417
+
351
418
  if __name__ == '__main__':
352
419
 
353
420
  while True:
@@ -355,5 +422,5 @@ if __name__ == '__main__':
355
422
  # proxies = {"https": proxy_ip,
356
423
  # "http": proxy_ip}
357
424
  time_out = 10 # Set the timeout value
358
- result = repeated_acquisition_ask_async(time_out, 5800, 6, 10)
425
+ result = get_stock_real_time_quotes(time_out, 10)
359
426
  print(result)
@@ -45,7 +45,6 @@ def get_proxy_api(order_id, secret, unbind_time):
45
45
  pid = str(list(random_row['pid'])[0])
46
46
 
47
47
  num = "1"
48
-
49
48
  noDuplicate = "1"
50
49
  lineSeparator = "0"
51
50
  singleIp = "0"
@@ -65,6 +64,7 @@ def get_proxy_api(order_id, secret, unbind_time):
65
64
  for dic in js_res["data"]:
66
65
  try:
67
66
  ip = dic["ip"]
67
+ # ip = dic["realIp"]
68
68
  port = dic["port"]
69
69
  ip_port = ip + ":" + str(port)
70
70
  return ip_port
@@ -62,6 +62,7 @@ def generate_proxy_ip_api(minutes):
62
62
  # 获取10分钟动态ip
63
63
  ip = liu_guan_proxy_api.get_proxy_api(order_id, secret, str(60 * minutes))
64
64
  try_numer = try_numer - 1
65
+ logger.info(ip)
65
66
  return ip
66
67
  except BaseException as e:
67
68
  time.sleep(1)
@@ -244,5 +245,8 @@ def import_province_and_city():
244
245
 
245
246
 
246
247
  if __name__ == "__main__":
247
- generate_proxy_ip(1)
248
- # get_proxy_ip_pool(1, 50, 2)
248
+ stock_account_info = get_account_cache()
249
+ order_id = list(stock_account_info['password'])[0]
250
+ secret = list(stock_account_info['account'])[0]
251
+ # 获取10分钟动态ip
252
+ ip = liu_guan_proxy_api.get_proxy_api(order_id, secret, str(60 * 1))
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mns-common
3
- Version: 1.5.2.5
3
+ Version: 1.5.2.6
4
4
 
@@ -16,10 +16,11 @@ mns_common/api/em/real_time/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94
16
16
  mns_common/api/em/real_time/east_money_debt_api.py,sha256=j6LnAG4bowEVXaor10zhao0kPksjH0mj4_tx_tNSH9o,17808
17
17
  mns_common/api/em/real_time/east_money_etf_api.py,sha256=wv1Ae3ydWDZkhlGgCKO8-z-PQj3qRSvoN75xNna4Qcs,19392
18
18
  mns_common/api/em/real_time/east_money_stock_a_api.py,sha256=6xYcNJyMhFFL2eNxQ9c0TbQ53wgumNovVHisXgQX7bs,12737
19
- mns_common/api/em/real_time/east_money_stock_a_v2_api.py,sha256=mL4uuL6sVsC2Vnl09826AUnzxePGAUhlZ7I5BBFw8Ks,14530
20
- mns_common/api/em/real_time/east_money_stock_hk_api.py,sha256=G_GYF2JvKtYK0J61ySiUtkRKQGeCYz5-WACmucbZfzY,18015
19
+ mns_common/api/em/real_time/east_money_stock_a_v2_api.py,sha256=v4c6oyutVE5345whT-scEduksi9Svgoov6A-1FYLikg,18354
20
+ mns_common/api/em/real_time/east_money_stock_hk_api.py,sha256=FpL9tOLyl-hs9vrvw3SFDxSY7n0Da90FO8Amo54ctJM,10541
21
+ mns_common/api/em/real_time/east_money_stock_hk_gtt_api.py,sha256=GEialRF41hnZWBL4ueESM7MMl0U_ARAoNSt2popOAEU,8248
21
22
  mns_common/api/em/real_time/east_money_stock_us_api.py,sha256=RiTrdZDuDgTOtiMSD1Ba9aQAx4vghM66pEp_LicH3Ps,11632
22
- mns_common/api/em/real_time/real_time_quotes_repeat_api.py,sha256=PKN2a_8fZpb7fA2Ns3FxVYiYlh34uxEFmmZu2MdqAu4,16008
23
+ mns_common/api/em/real_time/real_time_quotes_repeat_api.py,sha256=rQTUAkMvpOGGgZAXU2EdEufmxIApatt5T3WMXLuDA3o,18094
23
24
  mns_common/api/hk/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
24
25
  mns_common/api/hk/ths_hk_company_info_api.py,sha256=Cxlbuccopa0G1s8o0uTnnyLn2QaxOvbDpJQJOj7J8a8,5360
25
26
  mns_common/api/k_line/__init__.py,sha256=itoGlqKhsx7EVXQoD1vchDKQ5GPB16vDjofTSuQtrXg,161
@@ -44,7 +45,7 @@ mns_common/api/kpl/symbol/symbol_his_quotes_api.py,sha256=5F9L8V2UI_YUYe2dO6FbVK
44
45
  mns_common/api/msg/__init__.py,sha256=2U9DiKslxsWwLLEcZKjS8UiQPN1QgALvnK3HiJNIZE0,163
45
46
  mns_common/api/msg/push_msg_api.py,sha256=z8jDqFWygfxnCFFfQp4K-llgg27nRLv7Mx72lOddBH0,1390
46
47
  mns_common/api/proxies/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
47
- mns_common/api/proxies/liu_guan_proxy_api.py,sha256=lULS2ejxmVuM6t6PHBczvH-HjMJxiCYEDrCUAtci-t4,3730
48
+ mns_common/api/proxies/liu_guan_proxy_api.py,sha256=MLoq13xT0uffqUWSCemTwQZlF20YC7EIcr3PFeY8_p8,3762
48
49
  mns_common/api/qmt/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
49
50
  mns_common/api/qmt/qmt_minunte_tick_data.py,sha256=uwSw_AkA9RaD3pXPKzxqi4TKEkpglmFUwtYl9r5E6G8,3019
50
51
  mns_common/api/ths/__init__.py,sha256=2U9DiKslxsWwLLEcZKjS8UiQPN1QgALvnK3HiJNIZE0,163
@@ -117,7 +118,7 @@ mns_common/component/k_line/patterns/pattern_Enum.py,sha256=bl8cH1H3BWdj_deVO124
117
118
  mns_common/component/price/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
118
119
  mns_common/component/price/trade_price_service_api.py,sha256=0loBjbOt__o-ngc2Q4n5lF8_0x2WINRpL-cH1341Uaw,4396
119
120
  mns_common/component/proxies/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
120
- mns_common/component/proxies/proxy_common_api.py,sha256=f3jVFFVcKWOslQre8gqpYWT6UlZIXp6rWofgVifgGIg,7945
121
+ mns_common/component/proxies/proxy_common_api.py,sha256=Q7rnY3uceJmyXtG2rGLKnXUctSpkno-lrrQJIGUW2sI,8172
121
122
  mns_common/component/qmt/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
122
123
  mns_common/component/qmt/qmt_buy_service.py,sha256=tLTgrSxCcxuMhADRBBrW4ZWR_3MdbMZvvMdH5hbwyJU,7190
123
124
  mns_common/component/real_time/__init__.py,sha256=2U9DiKslxsWwLLEcZKjS8UiQPN1QgALvnK3HiJNIZE0,163
@@ -159,7 +160,7 @@ mns_common/utils/date_handle_util.py,sha256=XS-MyA8_7k35LOCFAYOHgVcVkMft_Kc4Wa9U
159
160
  mns_common/utils/db_util.py,sha256=hSmfNAN4vEeEaUva6_cicZEhb2jSnib-Gvk2reke1vc,2590
160
161
  mns_common/utils/file_util.py,sha256=egWu6PenGPRp_ixrNTHKarT4dAnOT6FETR82EHUZJnQ,1042
161
162
  mns_common/utils/ip_util.py,sha256=UTcYfz_uytB__6nlBf7T-izuI7hi4XdB6ET0sJgEel4,969
162
- mns_common-1.5.2.5.dist-info/METADATA,sha256=OWewhOP5SS-XiA89F8u3XfrKbnCq9uV2bfB22voSnw4,61
163
- mns_common-1.5.2.5.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
164
- mns_common-1.5.2.5.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
165
- mns_common-1.5.2.5.dist-info/RECORD,,
163
+ mns_common-1.5.2.6.dist-info/METADATA,sha256=GJmnp21FOIx5_GpaK3PHO1KDcEPiqslmBnqzvd0Ww6E,61
164
+ mns_common-1.5.2.6.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
165
+ mns_common-1.5.2.6.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
166
+ mns_common-1.5.2.6.dist-info/RECORD,,