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

如何编写VBA脚本抓取网站数据?Excel VBA新手代码求助

Hey there! 作为刚入坑Excel VBA的新手,要做网页产品数据抓取确实容易踩坑,我来帮你梳理下落地思路,一步步解决问题~

核心问题拆解与分步解决方案

第一步:先摸透目标网站的「脾气」

首先得确认两个关键信息:

  1. 目标网站是否允许爬虫(可以看看网站的robots.txt或者用户协议,避免违规);
  2. 产品数据是静态HTML渲染还是动态JS加载(按F12打开浏览器开发者工具,切换到「网络」标签,刷新产品页,看数据是直接在HTML里,还是需要调用AJAX接口获取)。

第二步:用VBA发起网络请求的正确姿势

根据网站类型选对应的请求方式:

静态页面用XMLHTTP(轻量快速)

Sub GetStaticProductData()
    Dim xmlHttp As Object, htmlDoc As Object
    Dim productID As String, quantity As Integer
    Dim nextRow As Long
    
    ' 获取用户输入
    productID = InputBox("请输入产品编号/名称:")
    quantity = Val(InputBox("请输入采购数量:"))
    
    ' 初始化网络请求对象
    Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
    ' 替换成目标网站的产品页面URL,把productID拼接到参数里
    xmlHttp.Open "GET", "https://目标网站地址/product?code=" & productID, False
    ' 添加请求头模拟浏览器,避免被反爬
    xmlHttp.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/118.0.0.0 Safari/537.36"
    xmlHttp.send
    
    ' 解析返回的HTML
    Set htmlDoc = CreateObject("HTMLFile")
    htmlDoc.body.innerHTML = xmlHttp.responseText
    
    ' 定位产品数据示例(替换成目标网站的实际CSS选择器)
    Dim productName As String, material As String, size As String
    productName = htmlDoc.querySelector(".product-title").innerText
    material = htmlDoc.querySelector(".tech-specs .material").innerText
    size = htmlDoc.querySelector(".tech-specs .size").innerText
    
    ' 写入Excel工作表
    nextRow = ThisWorkbook.Sheets("产品数据").Cells(Rows.Count, 1).End(xlUp).Row + 1
    With ThisWorkbook.Sheets("产品数据")
        .Cells(nextRow, 1).Value = productID
        .Cells(nextRow, 2).Value = productName
        .Cells(nextRow, 3).Value = quantity
        .Cells(nextRow, 4).Value = material
        .Cells(nextRow, 5).Value = size
    End With
    
    ' 清理对象
    Set htmlDoc = Nothing
    Set xmlHttp = Nothing
    MsgBox "数据已成功写入!"
End Sub

动态页面用Edge/IE自动化(兼容JS渲染)

如果产品数据是JS动态加载的,XMLHTTP抓不到完整内容,就用浏览器自动化:

Sub GetDynamicProductData()
    Dim edge As Object, productID As String, quantity As Integer
    Dim nextRow As Long
    
    productID = InputBox("请输入产品编号/名称:")
    quantity = Val(InputBox("请输入采购数量:"))
    
    ' 初始化Edge浏览器对象
    Set edge = CreateObject("Microsoft.Edge.Application")
    edge.Visible = True ' 设为False可以后台运行
    edge.Navigate "https://目标网站地址/search?keyword=" & productID
    
    ' 等待页面完全加载
    Do While edge.Busy Or edge.ReadyState <> 4
        DoEvents
    Loop
    
    ' 定位数据示例(替换成实际元素选择器)
    Dim productName As String, tensileStrength As String
    productName = edge.Document.querySelector(".product-name").innerText
    tensileStrength = edge.Document.querySelector(".tech-item.tensile").innerText
    
    ' 写入Excel
    nextRow = ThisWorkbook.Sheets("产品数据").Cells(Rows.Count, 1).End(xlUp).Row + 1
    With ThisWorkbook.Sheets("产品数据")
        .Cells(nextRow, 1).Value = productID
        .Cells(nextRow, 2).Value = productName
        .Cells(nextRow, 3).Value = quantity
        .Cells(nextRow, 4).Value = tensileStrength
    End With
    
    ' 关闭浏览器并清理
    edge.Quit
    Set edge = Nothing
    MsgBox "数据抓取完成!"
End Sub

第三步:常见问题排查技巧

  • 如果返回的HTML是空的:大概率是网站的反爬机制,试试添加Cookie、Referer等请求头,或者换用浏览器自动化方式;
  • 如果元素定位失败:用浏览器开发者工具的「选择元素」功能,复制准确的CSS选择器或ID,确认元素不是动态生成的(如果是,需要增加等待时间);
  • 如果Excel写入报错:检查工作表名称是否正确,或者是否有单元格保护。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:34:56