无法在我的scraper中设置超时选项以将其从无限循环中保存

时间:2018-05-16 18:27:22

标签: vba excel-vba web-scraping timeout internet-explorer-11

我已经在vba中编写了一个脚本,使用IE在其搜索框的网页中启动搜索,通过点击搜索按钮根据搜索填充结果。网页加载searchbox几秒钟后它就会打开。但是,我的下面的脚本可以处理此障碍并以正确的方式执行搜索。

现在,我有一个稍微不同的问题:假设我已经定义了一个错误的ID名称,或者该网页中没有ID,那么我和#39;我写的脚本将永远循环。这当然是一个严重的问题。

如何修改我的脚本在循环中放置一些timeout选项,以便刮刀等待某个时间元素出现,如果元素在该时间内不存在则会突然出现循环并退出浏览器。提前谢谢。

这是我的剧本:

Sub GetItems()
    Dim HTML As HTMLDocument, post As Object

    With New InternetExplorer
        .Visible = True
        .navigate "https://torrentz2.eu/"
        While .Busy Or .ReadyState < 4: DoEvents: Wend
        Set HTML = .Document

        With HTML
            Do     ''Wish to set timeout options within this loop to save it from infinite looping
                Set post = .getElementById("thesearchbox")
                DoEvents
            Loop While post Is Nothing

            post.innerText = "Udemy"

            .getElementById("thesearchbutton").Click
        End With
        .Quit
    End With
End Sub

1 个答案:

答案 0 :(得分:3)

尝试更换:

    With HTML
        Do     ''Wish to set timeout options within this loop to save it from infinite looping
            Set post = .getElementById("thesearchbox")
            DoEvents
        Loop While post Is Nothing

        post.innerText = "Udemy"

        .getElementById("thesearchbutton").Click
    End With

Dim dTime as Single ' as pointed out by @Mathieu Guindon in comments
'____________________________

dTime = Timer

With HTML
    Do     ''Wish to set timeout options within this loop to save it from infinite looping
        Set post = .getElementById("thesearchbox")
        If Timer - dTime > 60 Then Exit Do ' 60: num of seconds, equal 1 min
        DoEvents
    Loop While post Is Nothing

    post.innerText = "Udemy"

    .getElementById("thesearchbutton").Click
End With