You need to enable JavaScript to run this app. 如何从网站下载嵌入CSV文件:NSE期权链站点VBA下载失败求助
优惠活动
大模型
产品
解决方案
定价
更多

如何从网站下载嵌入CSV文件:NSE期权链站点VBA下载失败求助

失败原因
  • 你当前使用的QueryTable只能抓取页面初始静态内容,该站点的期权链数据需要手动选择「View Options Contracts for」「Expiry Date」两个下拉选项后,才会通过后端接口请求返回并渲染到页面,初始访问的页面没有对应目标数据
  • 站点配置了反爬校验,直接发送静态页面请求会被拦截,无法返回有效数据
  • 你指定抓取的第一个表格是页面初始加载的空占位表格,不是筛选后生成的目标数据
可行实现方案

推荐用VBA调用IE浏览器模拟人工操作的方式实现,操作逻辑完全对齐手动下载的流程,示例代码如下:

Option Explicit
Public Sub DownloadNSEOptionChainCSV()
    Dim IE As Object
    Dim ws As Worksheet
    Dim dropdownUnderlying As Object
    Dim dropdownExpiry As Object
    Dim downloadBtn As Object
    Dim targetUnderlying As String, targetExpiry As String
    
    ' 配置参数:修改为你需要的标的和到期日
    targetUnderlying = "NIFTY"
    targetExpiry = "27-Jun-2024" ' 要和站点下拉选项的日期格式完全一致
    
    Set ws = ThisWorkbook.Worksheets("Test")
    Set IE = CreateObject("InternetExplorer.Application")
    
    With IE
        .Visible = True ' 可以设为False后台运行,调试阶段建议设为True查看操作过程
        .navigate "https://www.nseindia.com/option-chain"
        
        ' 等待页面加载完成
        Do While .readyState <> 4 Or .Busy
            Application.Wait Now + TimeValue("00:00:02")
        Loop
        
        ' 等待下拉框元素加载
        Application.Wait Now + TimeValue("00:00:03")
        
        ' 选择标的下拉框
        Set dropdownUnderlying = .document.getElementById("underlyingSelect")
        If Not dropdownUnderlying Is Nothing Then
            Dim opt As Object
            For Each opt In dropdownUnderlying.Options
                If opt.Text = targetUnderlying Then
                    opt.Selected = True
                    Exit For
                End If
            Next
            ' 触发下拉框变更事件
            dropdownUnderlying.FireEvent ("onchange")
            Application.Wait Now + TimeValue("00:00:03")
        End If
        
        ' 选择到期日下拉框
        Set dropdownExpiry = .document.getElementById("expirySelect")
        If Not dropdownExpiry Is Nothing Then
            For Each opt In dropdownExpiry.Options
                If opt.Text = targetExpiry Then
                    opt.Selected = True
                    Exit For
                End If
            Next
            dropdownExpiry.FireEvent ("onchange")
            ' 等待数据加载完成
            Application.Wait Now + TimeValue("00:00:05")
        End If
        
        ' 点击下载CSV按钮
        Set downloadBtn = .document.getElementById("downloadOCTable")
        If Not downloadBtn Is Nothing Then
            downloadBtn.Click
            ' 等待下载完成
            Application.Wait Now + TimeValue("00:00:03")
        End If
        
        .Quit
    End With
    
    Set IE = Nothing
    MsgBox "CSV下载请求已触发,请检查你的浏览器默认下载路径", vbInformation
End Sub
注意事项
  • 运行前请确保IE浏览器可以正常访问目标站点,首次运行建议把IE.Visible设为True,确认操作流程符合预期
  • 代码里的等待时间可以根据你的网络速度适当调整,网络较慢的话可以把等待时间加长
  • 标的名称和到期日要和站点下拉选项的文本完全一致,否则会匹配不到对应选项
  • 如果提示元素找不到,大概率是页面还没加载完成,加长等待时间即可解决

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 02:27:03