使用VBA从网页表中提取值

问题描述 投票:0回答:1

我需要从以下网站的表格中提取一些值:https://www.anbima.com.br/informacoes/indicadores/.

我尝试使用使用VBA从网页中提取表格中给出的解决方案,但都不适合我。

我对VBA比较陌生,对java也不熟悉。有没有办法只提取值 6.869,14 和 0,35。两者都在最后一栏。

html excel vba web-scraping
1个回答
0
投票

也许这可以帮助您开始:

Sub ScrapeWebsite()
'Declare variables
Dim objHTTP As New WinHttp.WinHttpRequest
Dim htmlDoc As New HTMLDocument
Dim htmlElement As IHTMLElement

Dim url As String
Dim readNextCell As Boolean
Dim IPCA As Boolean
Dim rowSpan As Integer

readNextCell = False
IPCA = False
rowSpan = 0
'Set the URL to be scraped
url = "https://www.anbima.com.br/informacoes/indicadores/"
'Make a request to the URL
objHTTP.Open "GET", url, False
objHTTP.send
'Parse the HTML response
htmlDoc.body.innerHTML = objHTTP.responseText
'Loop through the HTML elements and extract data


For Each aRow In htmlDoc.getElementsByClassName("linhadados") '.getElementsByTagName("tr")
'Do something with the data, e.g. print it to the Immediate window
    For Each acell In aRow.getElementsByTagName("td")
    If readNextCell Then
        MsgBox acell.innerText
        readNextCell = False
    End If
    If IPCA And (acell.innerText = "Número Índice" Or acell.innerText = "Projeção (abr/24)") Then
        readNextCell = True
        IPCA = False
    End If
    If (acell.innerText = "IPCA (mar/24)6" Or acell.innerText = "IPCA1") Then
        IPCA = True
        rowSpan = acell.rowSpan
    End If
    Next acell
Next aRow
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.