我正在使用VBScript和getElementsByClassName
将数据从HTML传输到Excel。不幸的是,一个网站已经改变了他们的编码,所以现在我不确定如何在课堂上获取数据,因为它现在被分成几个部分。
页面源代码如下:
<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
但这并没有将类中的值分开。
有没有办法在课堂上获得每个“i data-recommendation-count”-value?
答案 0 :(得分:1)
getElementsByClassName("mod-tearsheet-recommendation__visual__column")
返回span元素的集合,这些元素是集合的项目。每个span都有5个childNodes。 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
值的三种方法。我找到这些值的方法是在每个引用之后设置一个断点,然后深入到本地窗口中的引用属性。接下来我会尝试在imediate窗口中测试这些属性。
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