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

如何用VBA构建网络爬虫提取动态链接与表格条目并导入Excel

动态渲染页面的链接与表格爬取方案

你遇到的问题是页面内容靠JavaScript动态生成,直接用WinHttp只能拿到带占位符的静态源码,根本抓不到实际的链接和表格数据。下面给两种适配你需求的解决办法:


一、VBA浏览器自动化(一键运行宏)

用VBA调用浏览器加载页面,等JS渲染完再抓现成的DOM内容,这样就能拿到真实的链接和表格数据。

直接可用的代码

Sub CrawlDBADRData()
    Dim ie As Object
    Dim targetTable As Object, tableRow As Object, tableCell As Object
    Dim targetSheet As Worksheet
    Dim currentRow As Long, colIndex As Long
    
    ' 准备工作表,清空旧数据(如果需要保留表头,可注释掉Clear)
    Set targetSheet = ThisWorkbook.Sheets("Sheet1")
    targetSheet.Cells.Clear
    
    ' 启动IE浏览器(也可以换成Edge,方法类似)
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True ' 改成False就能后台静默运行
    ie.navigate "https://www.adr.db.com/drwebrebrand/dr-universe/corporate_actions_type_e.html"
    
    ' 等页面完全加载(包括JS渲染)
    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop
    ' 给JS多留2秒渲染时间,防止页面加载慢漏数据
    Application.Wait Now + TimeValue("00:00:02")
    
    ' 定位目标表格(这里假设是页面第一个表格,可根据实际class/id调整,比如用querySelector)
    Set targetTable = ie.document.getElementsByTagName("table")(0)
    
    ' 遍历表格,把数据写入Excel
    currentRow = 1
    For Each tableRow In targetTable.Rows
        colIndex = 1
        For Each tableCell In tableRow.Cells
            ' 写入单元格文本
            targetSheet.Cells(currentRow, colIndex).Value = tableCell.innerText
            ' 如果单元格里有链接,提取实际href
            If tableCell.getElementsByTagName("a").Count > 0 Then
                targetSheet.Cells(currentRow, colIndex + 1).Value = tableCell.getElementsByTagName("a")(0).href
            End If
            colIndex = colIndex + 1
        Next tableCell
        currentRow = currentRow + 1
    Next tableRow
    
    ' 清理资源
    ie.Quit
    Set ie = Nothing
    Set targetTable = Nothing
    MsgBox "数据抓取完成!"
End Sub

小提示

  • 如果页面加载慢,可延长等待时间
  • 要是表格有特定class,比如class="adr-table",可以把定位代码改成Set targetTable = ie.document.querySelector(".adr-table"),更精准

二、Apify快速实现(不用写复杂代码)

Apify的可视化工具适合你这种需要每日重复抓取的场景:

  1. 注册Apify账号后,新建一个Web Scraper类型的Actor
  2. 在「Start URLs」里填入目标页面地址
  3. 进入「Page function」,用可视化选择器选中表格的每一行作为抓取项,再分别提取单元格文本和链接的href属性
  4. 配置每日定时运行,抓取完成后直接导出Excel文件,导入你的本地表格就行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 16:10:31