使用 getelement 获取具有多个值的 class
use getelement to get class with several values
我正在使用 VBScript 和 getElementsByClassName
从 HTML 到 Excel 获取数据。不幸的是,一个网站已经改变了他们的编码,所以现在我不确定如何获取 class 中的数据,因为它现在分为几个部分。
页面源代码如下所示:
<span class="mod-tearsheet-recommendation__visual__column">
<i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
<i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
<i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
<i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
<i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
</span>
我只对值 5、2、11、2、0 感兴趣。
http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ
我这样使用getElementByClassname
:
ws.Range("V2").Value = objExplorer.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")(1).innerHtml
但这不会分隔 class 中的值。
有没有办法获取 class 中的每个 "i data-recommendation-count" 值?
getElementsByClassName("mod-tearsheet-recommendation__visual__column")
正在返回一个 span 元素的集合,这些元素是集合的项目。每个跨度都有 5 个子节点。 childNodes 集合中的每个项目都有一个 data-recommendation-count
属性。
<span class="mod-tearsheet-recommendation__visual__column">
<i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
<i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
<i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
<i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
<i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
</span>
我向您展示了三种在下面的代码中引用 data-recommendation-count
值的方法。我定位这些值的方法是在每个引用后设置一个断点,然后深入到局部变量 window 中引用的属性。接下来我会尝试立即测试这些属性 window。
Sub SearchSite()
Dim i As Integer, j As Integer
Dim tearSheetsTags, tearsheet, dataCount
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ"
Do While objIE.Busy = True Or objIE.readyState <> 4
DoEvents
Loop
' <span class="mod-tearsheet-recommendation__visual__column">
' <i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
' <i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
' <i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
' <i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
' <i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
' </span>
Set tearSheetsTags = objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")
For Each tearsheet In tearSheetsTags
i = i + 1
j = 0
For Each dataCount In tearsheet.ChildNodes
j = j + 1
Cells(i, j) = dataCount.getAttribute("data-recommendation-count")
Next
Next
With tearSheetsTags
For i = 0 To .Length - 1
For j = 0 To .Item(i).ChildNodes.Length - 1
Cells(i + 7, j + 1) = .Item(i).ChildNodes.Item(j).getAttribute("data-recommendation-count")
Next j
Next i
End With
With objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")
With .Item(0)
Cells(13, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(13, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(13, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(13, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(13, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(1)
Cells(14, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(14, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(14, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(14, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(14, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(2)
Cells(15, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(15, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(15, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(15, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(15, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(3)
Cells(16, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(16, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(16, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(16, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(16, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(4)
Cells(17, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(17, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(17, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(17, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(17, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
End With
objIE.Quit
End Sub
我正在使用 VBScript 和 getElementsByClassName
从 HTML 到 Excel 获取数据。不幸的是,一个网站已经改变了他们的编码,所以现在我不确定如何获取 class 中的数据,因为它现在分为几个部分。
页面源代码如下所示:
<span class="mod-tearsheet-recommendation__visual__column">
<i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
<i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
<i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
<i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
<i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
</span>
我只对值 5、2、11、2、0 感兴趣。
http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ
我这样使用getElementByClassname
:
ws.Range("V2").Value = objExplorer.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")(1).innerHtml
但这不会分隔 class 中的值。
有没有办法获取 class 中的每个 "i data-recommendation-count" 值?
getElementsByClassName("mod-tearsheet-recommendation__visual__column")
正在返回一个 span 元素的集合,这些元素是集合的项目。每个跨度都有 5 个子节点。 childNodes 集合中的每个项目都有一个 data-recommendation-count
属性。
<span class="mod-tearsheet-recommendation__visual__column">
<i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
<i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
<i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
<i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
<i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
</span>
我向您展示了三种在下面的代码中引用 data-recommendation-count
值的方法。我定位这些值的方法是在每个引用后设置一个断点,然后深入到局部变量 window 中引用的属性。接下来我会尝试立即测试这些属性 window。
Sub SearchSite()
Dim i As Integer, j As Integer
Dim tearSheetsTags, tearsheet, dataCount
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ"
Do While objIE.Busy = True Or objIE.readyState <> 4
DoEvents
Loop
' <span class="mod-tearsheet-recommendation__visual__column">
' <i data-recommendation-count="5" style="background-color:#458B00; height:25%"></i>
' <i data-recommendation-count="2" style="background-color:#74A840; height:10%"></i>
' <i data-recommendation-count="11" style="background-color:#777777; height:55%"></i>
' <i data-recommendation-count="2" style="background-color:#DF6060; height:10%"></i>
' <i data-recommendation-count="0" style="background-color:#CC0000; height:0%"></i>
' </span>
Set tearSheetsTags = objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")
For Each tearsheet In tearSheetsTags
i = i + 1
j = 0
For Each dataCount In tearsheet.ChildNodes
j = j + 1
Cells(i, j) = dataCount.getAttribute("data-recommendation-count")
Next
Next
With tearSheetsTags
For i = 0 To .Length - 1
For j = 0 To .Item(i).ChildNodes.Length - 1
Cells(i + 7, j + 1) = .Item(i).ChildNodes.Item(j).getAttribute("data-recommendation-count")
Next j
Next i
End With
With objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")
With .Item(0)
Cells(13, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(13, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(13, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(13, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(13, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(1)
Cells(14, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(14, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(14, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(14, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(14, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(2)
Cells(15, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(15, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(15, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(15, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(15, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(3)
Cells(16, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(16, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(16, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(16, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(16, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
With .Item(4)
Cells(17, 1) = .ChildNodes.Item(0).getAttribute("data-recommendation-count")
Cells(17, 2) = .ChildNodes.Item(1).getAttribute("data-recommendation-count")
Cells(17, 3) = .ChildNodes.Item(2).getAttribute("data-recommendation-count")
Cells(17, 4) = .ChildNodes.Item(3).getAttribute("data-recommendation-count")
Cells(17, 5) = .ChildNodes.Item(4).getAttribute("data-recommendation-count")
End With
End With
objIE.Quit
End Sub