2017-10-12 256 views
2

我在vba中使用IE很新,因此有時候我很難糾正我在編寫任何代碼從網上抓取數據時犯的任何錯誤。我已經編寫了一些代碼,點擊其首頁中提供的標題Microsoft computer training videos下的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 
+0

可你只需要使用'IE.navigate surl'接下來posts'後'? –

+0

@Victor K,我不明白你在循環完成後在循環後面使用'IE.navigate surl'的邏輯,如果沒有回到'surl'頁面,無處可去。 – SIM

+0

那麼,你說你想讓它在循環之後回溯。我解釋說,導航到開始。然後你想在哪裏找到它? –

回答

2

嘗試以下操作:

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 
+0

沒辦法,Tehscript !!!它像魔術一樣工作。我不會給你任何休息的機會。在業餘時間,請按照此鏈接以及https://stackoverflow.com/questions/46682740/avoiding-hardcoded-delay-and-creating-conditional-statement-to-shake-off-error – SIM

相關問題