vba宏innertext的html页面访问

时间:2015-08-08 08:04:10

标签: html vba innertext

Option Explicit

Sub Sample()
    Dim objIe As InternetExplorerMedium
    Dim xobj As Variant
    Dim links As Variant
    Dim link As String
    Dim lnk As Variant
    Dim rc As Integer
    Set objIe = New InternetExplorerMedium
    objIe.Visible = True
    objIe.navigate "C:\Users\SOURABH\Desktop\test.html"
    Do While objIe.Busy And Not objIe.readyState = 4
        Application.StatusBar = "Loading.."
        DoEvents
    Loop
    MsgBox objIe.document.body.innerHTML
    Set xobj = objIe.document.getElementByTagName("myDiv")
    Set xobj = xobj.getElementsByClassName("myTable").Item(0)
    Set xobj = xobj.getElementsByClassName("data")(0)
    For Each lnk In links
        MsgBox "lnk got accessed"
        If lnk.className = "myTable" Then
            Sheets("Sheet1").Cells(3, 1).Value = lnk(0).innerText
            Exit For
        End If
    Next
    MsgBox xobj.innerText
    Set xobj = Nothing

    objIe.Quit
    Set objIe = Nothing
End Sub

我的HTML:

<div id="myDiv">    
<table class="myTable">
        <tbody>
            <tr>
                <td>Text1:</td>
                <td class="data"><strong>0.51</strong></td>
            </tr>
            <tr>
                <td>Text2:</td>
                <td class="data"><strong>2199</strong></td>
            </tr>
        </tbody>
    </table>
</div>

我可以使用MsgBox命令看到innertext但是在innertext中获取classname时出现错误:

  

对象在set xobj = objIe.document.getElementByTagName(“myDiv”)行中不支持此属性

请帮助我找到获取“数据”类的内容的方法

1 个答案:

答案 0 :(得分:1)

您正试图通过TAGNAME访问它,在MyDiv的情况下,它是DIV。

我认为你想要的是:

Set xobj = objIe.document.getElementById("myDiv")

编辑其他问题
我试过这样做,但我在Mac上:(如果我去GUESS,我会说你应该尝试类似的东西:

For Each obj In xobj.getElementsByClassName("data") 
    MsgBox obj 
Next 

还可以使用调试器来破解MsgBox,你可以看到对象中的内容