我正在尝试将具有特定类名的div元素的数据传递给Excel,例如:
<div class="myClass">
<span>Text1</span>
<span>Text2</span>
</div>
我的VBA代码:
Sub GetData()
Dim oHtml As HTMLDocument
Dim oElement As Object
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.example.com/", False
.send
oHtml.body.innerHTML = .responseText
End With
For Each oElement In oHtml.getElementsByClassName("myClass")
Debug.Print oElement.Children(0).src
Next oElement
End Sub
这是返回错误: 运行时错误:'438':对象不支持此属性或方法。错误是在线Debug.Print ...
我已激活以下参考: Microsoft HTML对象库 Microsoft Internet Controls
我希望能够在第一个或第二个范围内选择文本并将其粘贴到单元格上,我该怎么做?
答案 0 :(得分:6)
这对我有用:
Dim oHtml As HTMLDocument
Dim oElement As Object
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.example.com", False
.send
oHtml.body.innerHTML = .responseText
End With
Set dados = oHtml.getElementsByClassName("myClass")(0).getElementsByTagName("span")
i = 0
For Each oElement In dados
Sheets("Sheet1").Range("A" & i + 1) = dados(i).innerText
i = i + 1
Next oElement
答案 1 :(得分:-1)
以下是我过去的表现:
Set oElement = oHtml.getElementsByClassName("myClass")
i = 0
While i < oElement.Length
Debug.Print oElement(i).innerText
i = i + 1
Wend
你可能想要innerHtml吗?