使用类标记从HTML获取文本

时间:2017-09-16 14:50:51

标签: excel vba excel-vba internet-explorer

我想从以下HTML代码中提取3/14/2017:

<div class="divListTableBodyCell" id="tdColumnPostDateCell">
<table class="tblListTableBodyCell">
<tr>
<td>
<div class="divListTableBodyLabel">3/14/2017</div>

我正在使用Excel VBA执行此操作并使用以下代码尝试测试我要提取的信息:

Sub CC()
Dim ie As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "Main" & "*" Then 
        Set ie = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next


extract1 = ie.document.getElementsByClassName("divListTableBodyLabel")(3).innerText

MsgBox extract1

页面上有多个divListTableBodyLabel实例,各种日期,所以我只是看看我是否可以让它们中的任何一个出现,然后我可以担心得到我想要的那个。我已经尝试了上面的所有id或类名,没有任何回报?

1 个答案:

答案 0 :(得分:0)

这里的某个人给了我以下代码来设置一个IE对象,它的工作非常有用。

Runnable

所以只需Function GetIE(sLocation As String) As Object Dim objShell As Object, objShellWindows As Object, o As Object Dim sURL As String Dim RetVal As Object Set RetVal = Nothing Set objShell = CreateObject("shell.application") Set objShellWindows = objShell.Windows For Each o In objShellWindows sURL = "" On Error Resume Next 'check the URL and if it's the one you want then 'assign it to the return value and exit the loop sURL = o.document.location On Error GoTo 0 If sURL Like "*" & sLocation & "*" Then Set RetVal = o Exit For End If Next o Set GetIE = RetVal End Function ,你就可以了。看看这是否有帮助。