使用getelement获取具有多个值的类 [英] use getelement to get class with several values
问题描述
我正在使用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感兴趣.
Sub SearchSite()Dim i作为整数,j作为整数昏暗的泪水标签,泪水表,数据计数昏暗的objIE作为InternetExplorer设置objIE = New InternetExplorerobjIE.Visible = TrueobjIE.navigate"http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ"在objIE.Busy = True或objIE.readyState<>时执行4DoEvents环形'< 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>设置tearSheetsTags = objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")对于tearSheetsTags中的每个泪片我=我+ 1j = 0对于tearsheet.ChildNodes中的每个dataCountj = j + 1单元格(i,j)= dataCount.getAttribute("data-recommendation-count")下一个下一个带有tearSheetsTags对于i = 0至.Length-1对于j = 0到.Item(i).ChildNodes.Length-1单元格(i + 7,j + 1)= .Item(i).ChildNodes.Item(j).getAttribute("data-recommendation-count")下一个j接下来我结束于使用objIE.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")带有.Item(0)单元格(13,1)= .ChildNodes.Item(0).getAttribute("data-recommendation-count")单元格(13,2)= .ChildNodes.Item(1).getAttribute("data-recommendation-count")单元格(13,3)= .ChildNodes.Item(2).getAttribute("data-recommendation-count")单元格(13,4)= .ChildNodes.Item(3).getAttribute("data-recommendation-count")单元格(13,5)= .ChildNodes.Item(4).getAttribute("data-recommendation-count")结束于使用.Item(1)单元格(14,1)= .ChildNodes.Item(0).getAttribute("data-recommendation-count")单元格(14,2)= .ChildNodes.Item(1).getAttribute("data-recommendation-count")单元格(14,3)= .ChildNodes.Item(2).getAttribute("data-recommendation-count")单元格(14,4)= .ChildNodes.Item(3).getAttribute("data-recommendation-count")单元格(14,5)= .ChildNodes.Item(4).getAttribute("data-recommendation-count")结束于使用.Item(2)单元格(15,1)= .ChildNodes.Item(0).getAttribute("data-recommendation-count")单元格(15,2)= .ChildNodes.Item(1).getAttribute("data-recommendation-count")单元格(15,3)= .ChildNodes.Item(2).getAttribute("data-recommendation-count")单元格(15,4)= .ChildNodes.Item(3).getAttribute("data-recommendation-count")单元格(15,5)= .ChildNodes.Item(4).getAttribute("data-recommendation-count")结束于使用.Item(3)单元格(16,1)= .ChildNodes.Item(0).getAttribute("data-recommendation-count")单元格(16,2)= .ChildNodes.Item(1).getAttribute("data-recommendation-count")单元格(16,3)= .ChildNodes.Item(2).getAttribute("data-recommendation-count")单元格(16,4)= .ChildNodes.Item(3).getAttribute("data-recommendation-count")单元格(16,5)= .ChildNodes.Item(4).getAttribute("data-recommendation-count")结束于使用.Item(4)单元格(17,1)= .ChildNodes.Item(0).getAttribute("data-recommendation-count")单元格(17,2)= .ChildNodes.Item(1).getAttribute("data-recommendation-count")单元格(17,3)= .ChildNodes.Item(2).getAttribute("data-recommendation-count")单元格(17,4)= .ChildNodes.Item(3).getAttribute("data-recommendation-count")单元格(17,5)= .ChildNodes.Item(4).getAttribute("data-recommendation-count")结束于结束于objIE.Quit结束子
I am using VBScript and getElementsByClassName
to get data from HTML to Excel. Unfortunately, a site has changed their coding, so now I am unsure how to get the data in the class, since it is now divided into several parts.
The page source looks like this:
<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>
I am interested only in the values 5, 2, 11, 2, 0.
http://markets.ft.com/data/equities/tearsheet/forecasts?s=MMM:NYQ
I use getElementByClassname
like this:
ws.Range("V2").Value = objExplorer.document.getElementsByClassName("mod-tearsheet-recommendation__visual__column")(1).innerHtml
But this doesn't separate the values in the class.
Is there a way to get each "i data-recommendation-count"-value in the class?
getElementsByClassName("mod-tearsheet-recommendation__visual__column")
is returning a collection of span elements which are items of the collection. Each of the span's has 5 childNodes. Each item in the childNodes collection has a data-recommendation-count
attribute.
<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>
I show you three ways to reference the data-recommendation-count
values in my code below. The way that I located these values was to set a break point after each reference and then drilled down into the reference's properties in the locals window. Next I would try test these properties in the imediate 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
这篇关于使用getelement获取具有多个值的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!