我对在 vba 中使用 IE 还很陌生,所以有时我很难纠正在编写从网络上抓取数据的代码时所犯的任何错误。我编写了一些代码,用于单击首页标题 Microsoft 计算机培训视频
下的 20 个链接中的每个视频链接。我希望单击每个链接,然后返回并重复该过程,直到单击所有链接。我在抓取工具中定义的类名称和标签名称是准确的。我需要做的就是以正确的方式执行该过程。此时,我的抓取工具正在单击 20 个链接中的最后一个链接,并卡在它到达的地方,但不导航回来。
这就是我到目前为止所写的内容。
Sub clicking_links()
Const surl As String = "http://www.wiseowl.co.uk/videos/"
Dim IE As New InternetExplorer, iedoc As HTMLDocument
Dim posts As Object
With IE
.Visible = True
.navigate surl
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set iedoc = IE.document
End With
For Each posts In iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")
posts.getElementsByTagName("a")(0).Click
Next posts
End Sub
最佳答案
尝试以下操作:
Sub clicking_links()
Const surl As String = "http://www.wiseowl.co.uk/videos/"
Dim newurl as String
Dim IE As New InternetExplorer, iedoc As HTMLDocument
Dim posts As Object
Dim t As Long, i As Long
With IE
.Visible = True
.navigate surl
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set iedoc = .document
For Each posts In iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")
t = t + 1 'count the number of posts
Next posts
For i = 1 To t
Debug.Print i
newurl = iedoc.getElementsByClassName("woVideoListDefaultSeriesTitle")(i - 1).getElementsByTagName("a")(0).href
Debug.Print newurl
.navigate newurl
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set iedoc = .document
'here do your stuff within the new url
.navigate surl 'back to old url
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set iedoc = .document
Next i
End With
End Sub
关于vba - 点击链接后如何导航回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717876/