我想知道是否有一个 python 模块,我可以获取其期权交易量最高的股票、指数或 etf 的代码。 我尝试了 yfinance 来列出期权数量最多的资产,但这样做非常困难。
在下面的代码中,我尝试找到“间谍”股票。
import yfinance as yf
tickers = yf.Tickers('SPY')
all_stocks = tickers.tickers
for i in all_stocks:
print(i)
然后,我打算建立一个函数来检索'spy'的每只股票的数量,但这似乎太耗时了。
因此,我需要另一个模块,但这次是为了列出拥有更多期权的资产
我尝试使用 yfinance,但我必须遍历一个指数的所有股票,然后按数量排序
您可以使用请求和 BeautifulSoup 库从雅虎财经网站上抓取最活跃的选项。
import yfinance as yf
import requests
from bs4 import BeautifulSoup
# Scrape the Yahoo Finance Most Active Options page
url = "https://finance.yahoo.com/most-active"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
# Extract the tickers
table = soup.find('table', {'class': 'W(100%)'})
rows = table.find_all('tr')
most_active_tickers = [row.find('a').text for row in rows[1:]]
# Print the most active tickers
print("Most active stock options:")
for ticker in most_active_tickers:
print(ticker)
# Get information about the most active ticker
most_active_stock = yf.Ticker(most_active_tickers[0])
info = most_active_stock.info
print(f"\nInformation about {most_active_tickers[0]}:")
print(info)