首页 > 解决方案 > VBA:从 HTMLTable 中抓取确切的元素

问题描述

请你能帮我理解如何aTable类名中找到标签bptable吗?

我收到Object does not support this method了,但我不知道如何解决这个问题。

Sub ListVideosOnPage(VidCatName As String, VidCatURL As String)

Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument

Dim VidRow As MSHTML.IHTMLElement
Dim VidInnerRow As MSHTML.IHTMLElement


Dim VidRows As MSHTML.IHTMLElementCollection
Dim VidInnerRows As MSHTML.IHTMLElementCollection


Dim VidInnerCatID As Integer

XMLReq.Open "GET", VidCatURL, False
XMLReq.send

If XMLReq.Status <> 200 Then
        MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
        Exit Sub
End If

HTMLDoc.body.innerHTML = XMLReq.responseText
Set XMLReq = Nothing

Set VidRows = HTMLDoc.getElementsByClassName("bptable")
Set VidInnerRows = ***VidRows***.getElementsByTagName("a")
   With VidRows
        For VidInnerCatID = 2 To VidInnerRows.Length
                    Set VidInnerRow = VidInnerRows(VidInnerCatID)                                                
                    'Debug.Print                      
        Next VidInnerCatID
    End With
End Sub

标签: htmlvbaweb-scraping

解决方案


除了使用.getElementsByTagName("a")元素集合之外,您还可以使用VidRows(0)或尝试for loop获取单个元素以应用于.getElementsByTagName("a")它们。我会选择一个for loop来达到这个目的。以下是获取内容的一种方式。

Sub ListVideosOnPage(VidCatName As String, VidCatURL As String)

    Dim XMLReq As New XMLHTTP60
    Dim HTMLDoc As New HTMLDocument
    Dim VidInnerRows As Object
    Dim R As Long

    XMLReq.Open "GET", VidCatURL, False
    XMLReq.send

    If XMLReq.Status <> 200 Then
        MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
        Exit Sub
    End If

    HTMLDoc.body.innerHTML = XMLReq.responseText
    Set XMLReq = Nothing

    For Each VidInnerRows In HTMLDoc.getElementsByClassName("bptable")
        With VidInnerRows.getElementsByTagName("a")
            If .Length Then R = R + 1: Cells(R, 1) = .Item(0).innerText
        End With
    Next VidInnerRows
End Sub

推荐阅读