I am working on an option quotes downloader from finance yahoo using Python 3.9 with asyncio, aiohttp:
Could you advise how to find this bottleneck? Here is the code:
def get_tasks(session, tickers):
tasks = []
for ticker in tickers:
print(ticker)
url_options1 = f"https://query2.finance.yahoo.com/v7/finance/options/{ticker}?"
tasks.append(asyncio.create_task(session.get(url_options1, headers={'User-Agent': ua}, ssl=True)))
return tasks
def get_tasks2(session, ticker, expiries):
tasks2 = []
for expiry in expiries:
url_options2 = f"https://query2.finance.yahoo.com/v7/finance/options/{ticker}?&date={expiry}"
tasks2.append(asyncio.create_task(session.get(url_options2, headers={'User-Agent': ua}, ssl=True)))
return tasks2
async def get_optiondata(tickers):
results1 = []
async with aiohttp.ClientSession() as session:
tasks = get_tasks(session, tickers)
responses = await asyncio.gather(*tasks)
for response in responses:
results1.append(await response.json())
return results1
async def get_optiondata2(ticker, expiries):
results2 = []
async with aiohttp.ClientSession() as session:
tasks2 = get_tasks2(session, ticker, expiries)
responses = await asyncio.gather(*tasks2)
for response in responses:
results2.append(await response.json())
return results2
datas = asyncio.run(get_optiondata(usTickers))
results = []
tickers = []
expiries = []
for i in range(len(datas)):
try:
symbol = datas[i]['optionChain']['result'][0]['underlyingSymbol']
tickers.append(symbol)
if datas[i]['optionChain']['result'][0]['expirationDates']:
expiries.append(datas[i]['optionChain']['result'][0]['expirationDates'])
except IndexError as e:
print(f"{i} {e}")
for i in range(len(tickers)):
datas2 = asyncio.run(get_optiondata2(tickers[i], expiries[i]), debug=True)
您可以修改该端点的查询参数以检索代码的所有到期日期:
url = 'https://query2.finance.yahoo.com/v7/finance/options/{ticker}?getAllData=true'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.