Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA.getElementsByTagName()不返回元素_Excel_Vba_Web Scraping - Fatal编程技术网

Excel VBA.getElementsByTagName()不返回元素

Excel VBA.getElementsByTagName()不返回元素,excel,vba,web-scraping,Excel,Vba,Web Scraping,我正在尝试读取关于EPL的投注数据。当我运行以下子元素时,elements.Length返回0 Sub PullBetfair() ' SOCCER Const soccerEPL As String = "https://www.betfair.com.au/exchange/plus/football/competition/10932509" ' EPL ' DECLARE INTERNET EXPLORER Dim ie As New Inter

我正在尝试读取关于EPL的投注数据。当我运行以下子元素时,elements.Length返回0

Sub PullBetfair()

    ' SOCCER
    Const soccerEPL  As String = "https://www.betfair.com.au/exchange/plus/football/competition/10932509"   ' EPL

    ' DECLARE INTERNET EXPLORER
    Dim ie As New InternetExplorer
    ie.Visible = False

    ' NAVIGATE TO URL
    ie.navigate soccerEPL

    ' LOOP UNTIL NAVIGATION COMPLETE
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    ' COLLECT HTML DOCUMENT
    Dim html As HTMLDocument
    Set html = ie.document

    ' CREATE COLLECTION OF ELEMENTS
    Dim elements As IHTMLElementCollection

    Set elements = html.getElementsByTagName("section")
    Debug.Print elements.Length

    ie.Quit
    Set ie = Nothing
End Sub
我已经成功地从其他站点收集了数据,例如使用此方法,但是没有使用此站点

我在另一个网站上看到了一些关于框架的东西。HTML对我来说是新的,所以我不能完全理解它在说什么

我还尝试使用.getElementsByClassName收集元素,但没有成功

一个理想的答案可能会解释层次结构,这样我就可以理解如何深入到我试图阅读的表行中


非常感谢

以下使用适当的等待和定时循环测试长度

Option Explicit  
Public Sub TestForTags()
    Dim ie As New InternetExplorer, sections As Object, t As Date
     Const MAX_WAIT_SEC As Long = 10
    With ie
        .Visible = True
        .Navigate2 "https://www.betfair.com.au/exchange/plus/football/competition/10932509"
        While .Busy Or .readyState < 4: DoEvents: Wend
         t = Timer
        Do
            Set sections = ie.document.querySelectorAll("section")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While sections.Length = 0

        Debug.Print sections.Length
        Stop '<== Delete me later
        '.Quit
    End With
End Sub
选项显式
公共子测试FORTAGS()
Dim ie作为新的InternetExplorer,节作为对象,t作为日期
常量最大等待时间=10秒
与ie
.Visible=True
.导航2“https://www.betfair.com.au/exchange/plus/football/competition/10932509"
当.Busy或.readyState<4:DoEvents:Wend时
t=计时器
做
Set sections=ie.document.queryselectoral(“section”)
如果定时器-t>最大等待时间,则退出Do
在节段中循环。长度=0
调试。打印节。长度

Stop'以下使用适当的等待和定时循环测试长度

Option Explicit  
Public Sub TestForTags()
    Dim ie As New InternetExplorer, sections As Object, t As Date
     Const MAX_WAIT_SEC As Long = 10
    With ie
        .Visible = True
        .Navigate2 "https://www.betfair.com.au/exchange/plus/football/competition/10932509"
        While .Busy Or .readyState < 4: DoEvents: Wend
         t = Timer
        Do
            Set sections = ie.document.querySelectorAll("section")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While sections.Length = 0

        Debug.Print sections.Length
        Stop '<== Delete me later
        '.Quit
    End With
End Sub
选项显式
公共子测试FORTAGS()
Dim ie作为新的InternetExplorer,节作为对象,t作为日期
常量最大等待时间=10秒
与ie
.Visible=True
.导航2“https://www.betfair.com.au/exchange/plus/football/competition/10932509"
当.Busy或.readyState<4:DoEvents:Wend时
t=计时器
做
Set sections=ie.document.queryselectoral(“section”)
如果定时器-t>最大等待时间,则退出Do
在节段中循环。长度=0
调试。打印节。长度

Stop'我编写了这个用于加载页面的函数。有时我发现页面刷新不正常,被卡住&从未真正完成页面加载

它休眠100毫秒,然后检查页面是否已加载,如果在3秒钟后未完成刷新/加载,则刷新并重试

你会像这样使用它吗

ie.navigate "google.com"
waitforietoload ie 
这需要在模块的顶部

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Option Compare Text
Function waitForIEToLoad(ie As InternetExplorer)

Dim times, times2 As Integer

Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
    DoEvents
    Sleep 100
    times = times + 1
    If times = 30 Then
        ie.Refresh
        times2 = times2 + 1
        If times2 = 3 Then
            Exit Do
        End If
    End If
Loop

End Function
然后,在模块中的任何位置都会出现此问题

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Option Compare Text
Function waitForIEToLoad(ie As InternetExplorer)

Dim times, times2 As Integer

Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
    DoEvents
    Sleep 100
    times = times + 1
    If times = 30 Then
        ie.Refresh
        times2 = times2 + 1
        If times2 = 3 Then
            Exit Do
        End If
    End If
Loop

End Function
函数waitForIEToLoad(即作为InternetExplorer)
Dim times,times2为整数
在ie.readyState readyState_完成或ie忙时执行此操作
多芬特
睡100
次数=次数+1
如果时间=30,则
更新
times2=times2+1
如果时间2=3,则
退出Do
如果结束
如果结束
环
端函数

我编写了这个用于加载页面的函数。有时我发现页面刷新不正常,被卡住&从未真正完成页面加载

它休眠100毫秒,然后检查页面是否已加载,如果在3秒钟后未完成刷新/加载,则刷新并重试

你会像这样使用它吗

ie.navigate "google.com"
waitforietoload ie 
这需要在模块的顶部

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Option Compare Text
Function waitForIEToLoad(ie As InternetExplorer)

Dim times, times2 As Integer

Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
    DoEvents
    Sleep 100
    times = times + 1
    If times = 30 Then
        ie.Refresh
        times2 = times2 + 1
        If times2 = 3 Then
            Exit Do
        End If
    End If
Loop

End Function
然后,在模块中的任何位置都会出现此问题

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Option Compare Text
Function waitForIEToLoad(ie As InternetExplorer)

Dim times, times2 As Integer

Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
    DoEvents
    Sleep 100
    times = times + 1
    If times = 30 Then
        ie.Refresh
        times2 = times2 + 1
        If times2 = 3 Then
            Exit Do
        End If
    End If
Loop

End Function
函数waitForIEToLoad(即作为InternetExplorer)
Dim times,times2为整数
在ie.readyState readyState_完成或ie忙时执行此操作
多芬特
睡100
次数=次数+1
如果时间=30,则
更新
times2=times2+1
如果时间2=3,则
退出Do
如果结束
如果结束
环
端函数

我不是专家,但我是根据你的代码到处转悠的。这是我的小调整,它似乎工作,但不像预期的代码会崩溃,如果IE没有准备好。如果你能结合其他人关于测试IE是否准备好的答案,那就太好了

While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    mHour = Hour(Now())
    mMinute = Minute(Now())
    mSec = Second(Now()) + 1 'Wait one more second
    waitTime = TimeSerial(mHour, mMinute, mSec)
    Application.Wait waitTime
Wend    

...

Set elements = html.getElementsByTagName("tr")

    For i = 1 To elements.Length - 1 '
        Debug.Print elements(i).textContent
    Next i
当ie.Busy或ie.readyState readyState\u完成时
mHour=小时(现在())
mMinute=分钟(现在为()
毫秒=秒(现在())+1'再等一秒钟
waitTime=时间序列(mHour、mMinute、mSec)
申请,等一下
温德
...
Set elements=html.getElementsByTagName(“tr”)
对于i=1到元素。长度-1'
Debug.Print元素(i).textContent
接下来我

我不是专家,但我是根据你的代码到处转悠的。这是我的小调整,它似乎工作,但不像预期的代码会崩溃,如果IE没有准备好。如果你能结合其他人关于测试IE是否准备好的答案,那就太好了

While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    mHour = Hour(Now())
    mMinute = Minute(Now())
    mSec = Second(Now()) + 1 'Wait one more second
    waitTime = TimeSerial(mHour, mMinute, mSec)
    Application.Wait waitTime
Wend    

...

Set elements = html.getElementsByTagName("tr")

    For i = 1 To elements.Length - 1 '
        Debug.Print elements(i).textContent
    Next i
当ie.Busy或ie.readyState readyState\u完成时
mHour=小时(现在())
mMinute=分钟(现在为()
毫秒=秒(现在())+1'再等一秒钟
waitTime=时间序列(mHour、mMinute、mSec)
申请,等一下
温德
...
Set elements=html.getElementsByTagName(“tr”)
对于i=1到元素。长度-1'
Debug.Print元素(i).textContent
接下来我

HTML对您来说是新的!?,然而你却跳进了搜刮beting网站的行列。所有这些努力给你们带来了什么价值?这是因为readyState已经完成(4),而网站仍在加载。快速修复方法:在那里也设置延迟/睡眠。有些网站在readyState:complete和busy加载时会发生变化。当你变得更有经验时,你会学到这些东西。HTML对你来说是新的!?,然而你却跳进了搜刮beting网站的行列。所有这些努力给你们带来了什么价值?这是因为readyState已经完成(4),而网站仍在加载。快速修复方法:在那里也设置延迟/睡眠。有些网站在readyState:complete和busy加载时会发生变化。当你变得更有经验时,你会学到这些。等待的好方法!很好,等待的好方法!