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

寻求高效爬取纳斯达克4000+行机构持仓表方案(优先用Nasdaq API)

使用Nasdaq官方API爬取AAPL机构持仓数据

1. 核心API分析

Nasdaq的机构持仓数据通过官方结构化API提供,完全无需解析HTML页面。核心接口为:
https://api.nasdaq.com/api/quote/{symbol}/institutional-holdings
将{symbol}替换为目标股票代码(比如AAPL即可)。

该接口支持分页参数,用来处理4000+行的大量数据:

  • limit:每页返回的最大行数(最高可设为200,减少总请求次数)
  • offset:数据起始偏移量,用于控制翻页位置

2. Python实现代码

以下代码用requests库循环请求分页数据,自动获取所有持仓记录:

import requests
import pandas as pd
import time

def fetch_institutional_holdings(symbol):
    base_url = "https://api.nasdaq.com/api/quote/{}/institutional-holdings"
    # 模拟浏览器请求头,避免被API拦截
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
    }
    all_holdings = []
    offset = 0
    limit = 200  # 每页拉满200条,提升效率
    
    while True:
        params = {"limit": limit, "offset": offset}
        response = requests.get(base_url.format(symbol), headers=headers, params=params)
        response.raise_for_status()  # 捕获请求错误
        data = response.json()
        
        # 提取当前页的持仓数据
        current_page = data.get("data", {}).get("holdings", {}).get("rows", [])
        if not current_page:
            break  # 没有更多数据时停止循环
        
        all_holdings.extend(current_page)
        offset += limit
        print(f"已累计获取 {len(all_holdings)} 条数据")
        time.sleep(1)  # 加个小延迟,避免触发限流
    
    # 转换为DataFrame方便后续分析或保存
    df = pd.DataFrame(all_holdings)
    return df

# 调用函数获取AAPL的机构持仓数据
aapl_holdings = fetch_institutional_holdings("AAPL")
print(f"爬取完成,总数据行数:{len(aapl_holdings)}")
# 保存为CSV文件
aapl_holdings.to_csv("aapl_institutional_holdings.csv", index=False)

3. 关键细节说明

  • 请求头必填:必须携带User-Agent,否则API会返回403禁止访问,直接复制浏览器的UA即可
  • 分页逻辑:通过offset递增实现翻页,直到接口返回空数据列表,说明已获取全部记录
  • 数据结构:持仓数据存放在JSON的data.holdings.rows路径下,每条数据包含机构名称、持仓数量、持仓占比、持仓变动等完整字段
  • 效率优势:直接获取结构化JSON比解析HTML快数倍,4000+行数据只需十几次请求,全程无需处理复杂的DOM结构

4. 注意事项

  • 不要取消time.sleep(1)的延迟,过于频繁的请求会触发API限流,导致暂时无法访问
  • 若后续API返回结构有变动,只需调整代码中提取数据的JSON路径即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:15:14