getElementsBy()提取文本

时间:2018-02-19 01:20:50

标签: excel vba web-scraping getelementsbytagname getelementsbyclassname

我是VBA的新手,我一直试图将价值低于“Impuesto”栏目。

我收到错误438.我仍然不太明白如何引用网页的某个部分。

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

3 个答案:

答案 0 :(得分:1)

您需要getElementsByClassName()而不是getElementsByID,因为movimiento一词位于<li class="movimiento bg"> Impuesto </li>

Range("B1") = myIEDoc.getElementsByClassName("movimiento")(0).getElementsByClassName("l2 valor")(0)

编辑:

如果标记名称为<li>..</li>,请查看标记,以便getElementsByTagName("li")

如果代码包含id <li id="movimiento">..</li>,请查看代码,以便getElementByID("movimiento")

如果代码包含class <li class="movimiento">..</li>,请查看代码,以便getElementsByClassName("movimiento")

答案 1 :(得分:0)

尝试以下脚本。它应该获取您所追踪的数据。执行完成后,您应在电子表格中找到Range(“A1”)中的值。

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

答案 2 :(得分:0)

使用XMLHTTP请求的速度更快,如下所示:

Option Explicit
Public Sub GetInfo()
    Dim sResponse As String, html As HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://zonasegura1.bn.com.pe/TipoCambio/", False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With
    Set html = New HTMLDocument
    With html
        .body.innerHTML = sResponse
        Debug.Print .querySelector(".movimiento .l2.valor").innerText
    End With
End Sub