使用getelement来获得具有多个值的类

我正在使用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-tearheet-recommendation__visual__column”)(1).innerHtml

但这并不能区分课堂上的价值。

有没有办法在课堂上获得每个“我数据推荐计数”值?

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数值。 我find这些值的方法是在每个引用之后设置一个断点,然后在本地窗口中钻入引用的属性。 接下来,我会尝试在imediate窗口中testing这些属性。

在这里输入图像说明

 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