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

Excel VBA:抓取指定HTML中Compra与Venta对应数值

Excel VBA 抓取指定元素中的Compra和Venta数值

嘿,我来帮你搞定这个抓取需求!要从带有data-v-88f004c6属性的元素里提取“Compra:”和“Venta:”后面的数值,这里有两种实用的实现方案,你可以根据场景选择:


方法1:XMLHTTP + HTMLDocument(高效无浏览器)

这种方法不需要启动浏览器,速度快,适合批量静态页面抓取,代码如下:

Sub GetExchangeRates()
    Dim xmlHttp As Object
    Dim htmlDoc As Object
    Dim targetElements As Object
    Dim elem As Object
    Dim compraRate As String, ventaRate As String
    
    ' 创建核心对象
    Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
    Set htmlDoc = CreateObject("HTMLFile")
    
    On Error GoTo Cleanup ' 错误捕获
    
    ' 替换成你实际的目标网页URL
    xmlHttp.Open "GET", "你的目标网页地址", False
    xmlHttp.Send
    
    ' 加载返回的HTML内容
    htmlDoc.body.innerHTML = xmlHttp.responseText
    
    ' 精准筛选带指定data属性的span元素
    Set targetElements = htmlDoc.querySelectorAll("span[data-v-88f004c6]")
    
    ' 遍历元素提取目标数值
    For Each elem In targetElements
        If InStr(elem.innerText, "Compra:") > 0 Then
            compraRate = elem.getElementsByTagName("strong")(0).innerText
        ElseIf InStr(elem.innerText, "Venta:") > 0 Then
            ventaRate = elem.getElementsByTagName("strong")(0).innerText
        End If
    Next elem
    
    ' 将结果输出到Excel单元格(可自行调整位置)
    Range("A1").Value = "Compra汇率"
    Range("A2").Value = compraRate
    Range("B1").Value = "Venta汇率"
    Range("B2").Value = ventaRate
    
Cleanup:
    ' 释放资源
    Set xmlHttp = Nothing
    Set htmlDoc = Nothing
    Set targetElements = Nothing
    If Err.Number <> 0 Then
        MsgBox "抓取出错:" & Err.Description, vbExclamation
    End If
End Sub

关键细节:

  • querySelectorAll("span[data-v-88f004c6]"):直接定位所有符合属性要求的span元素,避免无效遍历
  • 通过InStr判断元素文本是否包含目标关键词,再提取<strong>标签内的数值
  • 务必替换代码里的你的目标网页地址为实际URL

方法2:Internet Explorer(适配动态渲染页面)

如果目标页面有JavaScript动态加载的内容,用IE模拟浏览器加载会更可靠,代码如下:

Sub GetRatesWithIE()
    Dim ie As Object
    Dim targetElements As Object
    Dim elem As Object
    Dim compraRate As String, ventaRate As String
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False ' 设为True可查看浏览器操作过程
    
    On Error GoTo Cleanup
    
    ' 打开目标网页
    ie.navigate "你的目标网页地址"
    
    ' 等待页面完全加载
    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop
    
    ' 筛选目标元素
    Set targetElements = ie.document.querySelectorAll("span[data-v-88f004c6]")
    
    ' 提取数值
    For Each elem In targetElements
        If elem.innerText Like "Compra:*" Then
            compraRate = elem.getElementsByTagName("strong")(0).innerText
        ElseIf elem.innerText Like "Venta:*" Then
            ventaRate = elem.getElementsByTagName("strong")(0).innerText
        End If
    Next elem
    
    ' 输出结果到Excel
    Range("A1").Value = "Compra汇率"
    Range("A2").Value = compraRate
    Range("B1").Value = "Venta汇率"
    Range("B2").Value = ventaRate
    
Cleanup:
    ie.Quit
    Set ie = Nothing
    Set targetElements = Nothing
    If Err.Number <> 0 Then
        MsgBox "出错:" & Err.Description, vbExclamation
    End If
End Sub

注意事项:

  • IE已逐步被淘汰,优先推荐方法1,仅在页面有动态渲染需求时使用此方法
  • 如果IE加载缓慢,可以适当增加等待时间或调整readyState判断逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:50:54