Skip to content

Commit 57c0937

Browse files
authored
Update get_top_sec_com.py
add async function.
1 parent ac3c78a commit 57c0937

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

spiderFile/get_top_sec_com.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
import os
33
import joblib
4+
import asyncio
5+
import aiohttp
46
import requests as rq
57

68
import pandas as pd
@@ -33,6 +35,26 @@ def get_sec_com_code(self):
3335
with open("useful_sec_com_list", "wb") as fp:
3436
joblib.dump(useful_sec_com_list, fp)
3537
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()]
3658

3759
def get_shares_details(self):
3860
all_shares = []
@@ -45,7 +67,8 @@ def get_shares_details(self):
4567
return all_shares
4668

4769
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()) # 异步代码
4972
df = pd.DataFrame(all_shares, columns=["股票代码", "公司", "市值(亿)"])
5073
df["市值(亿)"] = df["市值(亿)"].astype(float)
5174
df.sort_values(by="市值(亿)", ascending=False, inplace=True)

0 commit comments

Comments
 (0)