You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用yahooquery获取ETF全部持仓?无法实现时有无替代工具?

获取ETF全部持仓的解决方案

使用yahooquery或yfinance获取完整持仓

yahooquery

你当前用的fund_holding_info仅返回前10大持仓,换用fund_holdings属性可尝试获取更完整的持仓数据(数据范围取决于Yahoo Finance的公开内容):

from yahooquery import Ticker
import pandas as pd

t = Ticker("QQQ")
full_holdings = t.fund_holdings['QQQ']
df = pd.DataFrame(full_holdings)
print(f"持仓总数:{len(df)}")

如果返回结果仍只有10条,说明Yahoo Finance未公开该ETF的完整持仓数据,需通过其他途径获取。

yfinance

yfinance的fund_holdings同样默认返回前10大持仓,可尝试holdings属性获取更多数据:

import yfinance as yf
import pandas as pd

qqq = yf.Ticker("QQQ")
full_holdings = qqq.holdings
print(full_holdings)

需注意,yfinance与yahooquery共享同一数据源,若Yahoo Finance无完整数据,这两个库都无法直接返回全部持仓。

替代Python工具

若上述库无法满足需求,可尝试以下方案:

  • sec-edgar-api:直接从SEC的EDGAR数据库抓取ETF发行商的13F季度持仓报告,解析后得到完整持仓(需处理XML格式):

    from sec_edgar_api import EdgarClient
    import lxml.etree as ET
    
    edgar = EdgarClient(user_agent="Your Name your.email@example.com")
    # 获取Invesco(QQQ发行商)的最新13F报告
    filings = edgar.get_company_filings("Invesco Ltd", filing_type="13F-HR")
    latest_filing_url = filings[0]['filingUrl'] + "/xslForm13F_XML.xml"
    
    # 解析XML数据
    tree = ET.parse(latest_filing_url)
    root = tree.getroot()
    holdings = []
    for info_table in root.findall(".//infoTable"):
        holding = {
            "symbol": info_table.find("nameOfIssuer").text,
            "value": info_table.find("value").text
        }
        holdings.append(holding)
    df = pd.DataFrame(holdings)
    print(df)
    

    13F报告为季度更新,覆盖ETF的核心持仓,基本满足完整持仓需求。

  • pandas-datareader:尝试从Morningstar等数据源获取持仓数据(部分ETF支持完整数据):

    import pandas_datareader.data as web
    
    # 注意:Morningstar数据源可能存在访问限制
    holdings = web.DataReader('QQQ', 'morningstar')
    print(holdings)
    
  • 商业API封装库:如polygon-api-client或alpha_vantage,需申请API密钥,可提供实时、完整的ETF持仓数据:

    from polygon import RESTClient
    
    client = RESTClient(api_key="YOUR_API_KEY")
    etf_holdings = client.get_etf_holdings("QQQ")
    for holding in etf_holdings.holdings:
        print(f"{holding.ticker}: {holding.weight}%")
    

内容的提问来源于stack exchange,提问作者AlphaBetaGamma96

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:09:50