getElementsBy() 提取文本
getElementsBy() extract text
我是 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
您需要 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")
试试下面的脚本。它应该为您获取您想要的数据。执行完成后,您应该会在电子表格中找到 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
使用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
我是 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
您需要 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")
试试下面的脚本。它应该为您获取您想要的数据。执行完成后,您应该会在电子表格中找到 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
使用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