无法在我的抓取器中设置超时选项以将其保存为无限循环 [英] Unable to set timeout option within my scraper to save it from infinite looping

查看:182
本文介绍了无法在我的抓取器中设置超时选项以将其保存为无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a script in vba using IE to initiate a search in a webpage in it's searchbox to populate the result according to the search by hitting the search button. The webpage loads it's searchbox few seconds later it is made to open. However, my below script can handle this barrier and perform the search in the right way.

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

Now, I've got a slightly different question which is: suppose I've defined a wrong ID name or There is no such ID in that webpage then the way I've written my script will loop forever. This is certainly a serious issue.

如何修改我的脚本,在循环中添加一些timeout选项,以便抓取器将等待一段时间才能出现该元素,如果该元素在这段时间内不存在,则它将退出循环并退出浏览器.预先感谢.

How can I modify my script putting some timeout option within the loop so that the scraper will wait sometime for the element to appear and if the element is not present within that time then it will break out of the loop and quit the browser. Thanks in advance.

这是我的剧本:

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

推荐答案

尝试替换:

    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

这篇关于无法在我的抓取器中设置超时选项以将其保存为无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆