1
1
import re
2
2
import os
3
3
import joblib
4
+ import asyncio
5
+ import aiohttp
4
6
import requests as rq
5
7
6
8
import pandas as pd
@@ -33,6 +35,26 @@ def get_sec_com_code(self):
33
35
with open ("useful_sec_com_list" , "wb" ) as fp :
34
36
joblib .dump (useful_sec_com_list , fp )
35
37
return useful_sec_com_list
38
+
39
+ async def async_get_shares_details (self , sec_com , url ):
40
+ async with aiohttp .ClientSession () as session :
41
+ async with session .get (url , headers = self .headers ) as response :
42
+ html = await response .text ()
43
+ market_value = re .search ("<td>总市值:<span>(.*?)亿</span>" , html )
44
+ if market_value :
45
+ return [* sec_com , market_value .groups ()[0 ]]
46
+
47
+ async def async_get_all_shares (self ):
48
+ tasks = []
49
+ for sec_com in self .useful_sec_com_list :
50
+ url = self .shares_api + sec_com [0 ]
51
+ tasks .append (
52
+ asyncio .create_task (
53
+ self .async_get_shares_details (sec_com , url )
54
+ )
55
+ )
56
+ done , pendding = await asyncio .wait (tasks )
57
+ return [share .result () for share in done if share .result ()]
36
58
37
59
def get_shares_details (self ):
38
60
all_shares = []
@@ -45,7 +67,8 @@ def get_shares_details(self):
45
67
return all_shares
46
68
47
69
def yield_picture (self , save_path ):
48
- all_shares = self .get_shares_details ()
70
+ # all_shares = self.get_shares_details() # 同步代码
71
+ all_shares = asyncio .run (self .async_get_all_shares ()) # 异步代码
49
72
df = pd .DataFrame (all_shares , columns = ["股票代码" , "公司" , "市值(亿)" ])
50
73
df ["市值(亿)" ] = df ["市值(亿)" ].astype (float )
51
74
df .sort_values (by = "市值(亿)" , ascending = False , inplace = True )
0 commit comments