【问题标题】:Web scraping with Excel and VBA使用 Excel 和 VBA 进行网页抓取
【发布时间】:2017-05-28 20:17:36
【问题描述】:

我正在尝试提取团队名称,但我在这一行得到“需要运行时错误 424 对象”

Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container") 

,如果有人能指出我正确的方向,那就太好了。

    Sub useClassnames()
    Dim lists As IHTMLElementCollection
    Dim anchorElements As IHTMLElementCollection
    Dim ulElement As HTMLUListElement
    Dim liElement As HTMLLIElement
    Dim row As Long
    Dim ie As InternetExplorer

       Set ie = New InternetExplorer

           With ie.navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
           .Visible = True
      Do While ie.readyState <> READYSTATE_COMPLETE
      DoEvents
      Loop
      End With

      Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")

row = 1

     For Each ulElement In lists

       For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")

       Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")

          If anchorElements.Length > 0 Then
           Cells(row, 1) = anchorElements.Item(0).innerText
           row = row + 1
         End If
     Next liElement
     Next ulElement

   End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    错误 424 是 Object Required 错误。 Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container") 中的 Html 是什么?

    你需要的是

    Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
    

    我使用的代码

    Sub useClassnames()
        Dim lists As IHTMLElementCollection
        Dim anchorElements As IHTMLElementCollection
        Dim ulElement As HTMLUListElement
        Dim liElement As HTMLLIElement
        Dim row As Long
        Dim ie As InternetExplorer
    
        Set ie = New InternetExplorer
    
        With ie
            .navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
            .Visible = True
    
            Do While ie.readyState <> READYSTATE_COMPLETE
                DoEvents
            Loop
        End With
    
        Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
    
        row = 1
    
        For Each ulElement In lists
            For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")
                Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")
    
                If anchorElements.Length > 0 Then
                    Cells(row, 1) = anchorElements.Item(0).innerText
                    row = row + 1
                End If
            Next liElement
        Next ulElement
    End Sub
    

    截图

    【讨论】:

    • 我试图调整这个线程 stackoverflow.com/questions/41851152/…> 的响应,这似乎符合我的需要。它给了我同样的错误。
    • 我刚刚尝试了您在问题中发布的上述代码,它对我有用:)
    • 是的 :) 我刚刚在代码中将 Html 替换为 ie.document,它对我有用
    • 有趣,因为它给了我同样的错误。您能告诉我您使用了哪些参考文献吗?
    • 1Microsoft Internet Control 2Microsoft HTML 对象库
    猜你喜欢
    • 2016-09-12
    • 2020-11-30
    • 2019-04-03
    • 2017-11-04
    • 2013-08-27
    • 2021-01-19
    • 2019-07-30
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多