遍历对象HTML TableRowElement以获取其子项之一的innerText

时间:2014-04-09 14:53:26

标签: vba excel-vba excel

我编写了以下脚本,从这个webpage开始,应该能够提取包含在表字段中的值" Chg。 %"

Sub Test()

Dim getIE As Object, appIE As Object, indexValues As Object
Set getIE = CreateObject("InternetExplorer.application")
Set appIE = getIE

With appIE
.Navigate "http://www.investing.com/indices/world-indices"
.Visible = True
End With

Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 10)

Set indexValues = appIE.document.getElementById("pair_13376")

End Sub

变量indexValues[object HTMLTableRowElement];我猜,根据网页的结构,它包含值" -0.13%"我需要在第8 td.innerText

enter image description here

如何提取此值?我尝试过类似的东西:

i = 0
For Each td In indexValues
    i = i + 1
    If i = 8 Then
        myValue = td.innerText
    End If
Next 

但是,此方法失败,因为对象indexValues不是可迭代的对象列表。有人可以帮忙吗? 注意:我所指的索引是我发布的第一个网页,即" Merval"

2 个答案:

答案 0 :(得分:1)

以下行将为您提供所需内容。如果您想在该行上使用不同的值

,请更改索引号
     myValue = appIE.Document.getElementById("pair_13376").getElementsByTagName("td")(7).innertext

答案 1 :(得分:0)

运行此代码..这是您想要的...如果您需要更多帮助,请告诉我们。) * 经过测试 *

    Sub Test()

Dim getIE As Object, appIE As Object, indexValues As Object
Set getIE = CreateObject("InternetExplorer.application")
Set appIE = getIE

With appIE
.Navigate "http://www.investing.com/indices/world-indices"
.Visible = True
End With

Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 10)

 For Each tbl In getIE.document.getElementsByTagName("TABLE")
    '    tabno = tabno + 1
     '   If tabno <> 5 Then GoTo NxtTbl
            For Each rw In tbl.Rows
                nextrow = nextrow + 1
                Set Rng = Range("A" & nextrow)
                Cofs = 0
                I = 0
                For Each Cl In rw.Cells
                    I = I + 1
                    If I = Int(I / 3) * 3 Then GoTo NxtCl
                        Rng.Offset(, Cofs).Value = Cl.outerText
                        Cofs = Cofs + 1
NxtCl:
                 Next Cl
             Next rw
NxtTbl:
      Next tbl

      Range("C:E").Delete

End Sub