使用Excel和VBA进行Web抓取
我试图提取队名,但我得到了“运行时错误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
错误424是一个Object Required
错误。 什么是Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
Html
Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
?
你需要的是
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
截图