运行我的网站代码来抓取遍布多个页面的不同教程的标题,我发现它完美无瑕。我尝试编写一些代码,这些代码不依赖于url具有的最后页码,而是编写状态代码,直到它显示http.status<> 200。我在下面粘贴的代码在这种情况下无可挑剔。但是,当我尝试使用另一个url来查看它是否自动中断但发现代码确实获取了所有结果但没有中断时出现问题。在这种情况下,解决方法是什么,以便代码在完成时中断并停止宏?这是工作的吗?
Sub WiseOwl()
Const mlink = "http://www.wiseowl.co.uk/videos/default"
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim post As Object
Do While True
y = y + 1
With http
.Open "GET", mlink & "-" & y & ".htm", False
.send
If .Status <> 200 Then
MsgBox "It's done"
Exit Sub
End If
html.body.innerHTML = .responseText
End With
For Each post In html.getElementsByClassName("woVideoListDefaultSeriesTitle")
With post.getElementsByTagName("a")
x = x + 1
If .Length Then Cells(x, 1) = .item(0).innerText
End With
Next post
Loop
End Sub
我找到了解决黄页问题的逻辑。我的更新脚本能够解析黄页但在删除最后一页之前中断,因为没有&#34;下一页&#34;按钮。我试过这个: &#34; https://www.dropbox.com/s/iptqm79b0byw3dz/Yellowpage.txt?dl=0&#34;
然而,我尝试使用torrent网站的相同逻辑,但它在这里不起作用:
答案 0 :(得分:1)
如果元素退出或不退出,您始终可以依赖元素。例如,如果您尝试使用已设置元素的对象,则会得到:
运行时错误'91':对象变量或未设置块变量
这是您应该寻找的关键来结束您的代码。请参阅以下示例:
Sub yify()
Const mlink = "https://www.yify-torrent.org/genres/western/p-"
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim post As Object
Dim posts As Object
y = 1
Do
With http
.Open "GET", mlink & y & "/", False
.send
html.body.innerHTML = .responseText
End With
Set posts = html.getElementsByClassName("mv")
On Error GoTo Endofpage
Debug.Print Len(posts) 'to force Error 91
For Each post In posts
With post.getElementsByTagName("div")
x = x + 1
If .Length Then Cells(x, 1) = .Item(0).innerText
End With
Next post
y = y + 1
Endofpage:
Loop Until Err.Number = 91
Debug.Print "It's over"
End Sub