<pre class="lang-py prettyprint-override"><code>import httpx
import trio
import pandas as pd
codes = ["5090", "0105"]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"
}
async def worker(channel):
async with channel:
async for client, code in channel:
params = {
"securityCode": code
}
r = await client.get('https://www.malaysiastock.biz/Corporate-Infomation.aspx', params=params)
try:
df = pd.read_html(
r.text, attrs={"id": "ctl19_gvShareholdingChange"})[0]
except ValueError:
df = "N/A"
print("{}\nCode: {}\n{}".format("*" * 70, code, df))
async def main():
async with httpx.AsyncClient(timeout=None) as client, trio.open_nursery() as nurse:
client.headers.update(headers)
sender, receiver = trio.open_memory_channel(0)
async with receiver:
for _ in range(2):
nurse.start_soon(worker, receiver.clone())
async with sender:
for code in codes:
await sender.send([client, code])
if __name__ == "__main__":
trio.run(main)
</code></pre>
<p>输出:</p>
<pre><code>**********************************************************************
Code: 0105
Date of change Shares Director/Substantial Shareholder
0 23 Jun 2021 Acquired 200,000 MR LIM TECK SENG
1 22 Jun 2021 Acquired 200,000 MR LIM TECK SENG
2 23 Apr 2021 Disposed 750,000 MR LIM TECK SENG
3 19 Apr 2021 Acquired 2,040,000 DATO' YEO BOON LEONG
4 19 Apr 2021 Acquired 2,040,000 DATO' YEO BOON LEONG
5 16 Apr 2021 Acquired 1,080,000 DATO' YEO BOON LEONG
6 16 Apr 2021 Acquired 685,000 DATO' YEO BOON LEONG
7 16 Apr 2021 Acquired 1,080,000 DATO' YEO BOON LEONG
8 16 Apr 2021 Acquired 685,000 DATO' YEO BOON LEONG
9 14 Apr 2021 Acquired 50,000 MR LIM TECK SENG
**********************************************************************
Code: 5090
N/A
</code></pre>