无法在我的刮板中设置超时选项以将其从无限循环中保存
Unable to set timeout option within my scraper to save it from infinite looping
我在 vba 中编写了一个脚本,使用 IE 在网页的搜索框中启动搜索,通过点击搜索按钮根据搜索填充结果。网页加载 searchbox
几秒钟后打开。但是,我下面的脚本可以处理这个障碍并以正确的方式执行搜索。
现在,我有一个稍微不同的问题是:假设我定义了一个错误的 ID
名称,或者在该网页中没有这样的 ID
那么我的方式我写的脚本将永远循环。这当然是一个严重的问题。
我如何修改我的脚本,在循环中放置一些 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
尝试替换:
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
我在 vba 中编写了一个脚本,使用 IE 在网页的搜索框中启动搜索,通过点击搜索按钮根据搜索填充结果。网页加载 searchbox
几秒钟后打开。但是,我下面的脚本可以处理这个障碍并以正确的方式执行搜索。
现在,我有一个稍微不同的问题是:假设我定义了一个错误的 ID
名称,或者在该网页中没有这样的 ID
那么我的方式我写的脚本将永远循环。这当然是一个严重的问题。
我如何修改我的脚本,在循环中放置一些 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
尝试替换:
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