Search code examples
excelvbaweb-scrapinggetelementsbytagnamegetelementsbyclassname

getElementsBy() extract text


I'm really new to VBA and I've been trying to get the value below the Column "Impuesto".

I'm getting error 438. I still don't quite understand how to refer to a certain part of the web page.

Sub extract()

Dim myIE As Object
Dim myIEDoc As Object
Dim element As IHTMLElement

Set myIE = CreateObject("InternetExplorer.Application")

myIE.Visible = False

myIE.navigate "https://zonasegura1.bn.com.pe/TipoCambio/"

While myIE.Busy
    DoEvents
Wend
 Set myIEDoc = myIE.document


Range("B1") = myIEDoc.getElementsByID("movimiento")(0).getElementsByTagName("span")

End Sub

Solution

  • Try the below script. It should fetch you the data you are after. When the execution is done, you should find the value in Range("A1") in your spreadsheet.

    Sub Get_Quote()
        Dim post As Object
    
        With CreateObject("InternetExplorer.Application")
            .Visible = True
            .navigate "https://zonasegura1.bn.com.pe/TipoCambio/"
            While .Busy = True Or .readyState < 4: DoEvents: Wend
            Set post = .document.querySelector(".movimiento span.l2.valor")
            [A1] = post.innerText
            .Quit
        End With
    End Sub