2017-12-27 494 views
10

我已經寫在VBA刮刀解析從洪流站點的某些影片信息退出瀏覽器。我用IEqueryselector完成任務。當我執行我的代碼時,它會解析一切,並彈出一個錯誤。看起來這個錯誤似乎是無處不在,而不是繼續。如果我取消錯誤框,那麼我可以看到結果。我已經上傳了兩張圖片,向您展示我遇到的錯誤。我如何成功執行代碼而不會出現任何錯誤?提前致謝。我刮板拋出錯誤,而不是當一切都做

下面是完整的代碼:

Sub Torrent_Data() 
    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim post As Object 

    With IE 
     .Visible = False 
     .navigate "https://yts.am/browse-movies" 
     Do While .readyState <> READYSTATE_COMPLETE: Loop 
     Set html = .Document 
    End With 

    For Each post In html.querySelectorAll(".browse-movie-bottom") 
     Row = Row + 1: Cells(Row, 1) = post.queryselector(".browse-movie-title").innerText 
     Cells(Row, 2) = post.queryselector(".browse-movie-year").innerText 
    Next post 
    IE.Quit 
End Sub 

時遇到的錯誤:

First error

Second error

錯誤的兩者都出現在同一時間。 我使用Internet Explorer 11

在另一方面,如果我嘗試喜歡它下面沒有問題,成功帶來的結果。

Sub Torrent_Data() 
    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim post As Object 

    With IE 
     .Visible = False 
     .navigate "https://yts.am/browse-movies" 
     Do While .readyState <> READYSTATE_COMPLETE: Loop 
     Set html = .Document 
    End With 

    For Each post In html.getElementsByClassName("browse-movie-bottom") 
     Row = Row + 1: Cells(Row, 1) = post.queryselector(".browse-movie-title").innerText 
     Cells(Row, 2) = post.queryselector(".browse-movie-year").innerText 
    Next post 
    IE.Quit 
End Sub 

參考我已經添加到庫中:

1. Microsoft Internet Controls 
2. Microsoft HTML Object Library 

那麼,什麼是錯的queryselector或什麼,我在這裏失蹤,使一個成功的去嗎?是否有任何參考添加到圖書館擺脫錯誤?

+0

快速檢查:不改變'queryselector'到'querySelector'使第一個代碼示例中有什麼區別? – alecxe

+0

不,先生,沒有任何改變。事實上,如果我嘗試編寫'querySelector',它會自動回到'queryselector'的小寫字母。 – SIM

+0

好吧,試試這個:直接執行'post.innerText',而不是執行'post.queryselector'。這不是你想要做的,但讓我們試驗。你看到同樣的錯誤?謝謝。 – alecxe

回答

5

好了,也有一些是嚴重不友好有關的網頁。它一直在爲我崩潰。所以我已經採取在腳本引擎/腳本控制內運行JavaScript程序,它的工作原理。

我希望你能理解。該邏輯是在JavaScript中添加到ScriptEngine中的。我得到兩個節點列表,一個電影列表和一個年份列表;然後我逐步同步每個陣列並將它們作爲鍵值對添加到Microsoft Scripting Dictionary。

Option Explicit 

'*Tools->References 
'* Microsoft Scripting Runtime 
'* Microsoft Scripting Control 
'* Microsoft Internet Controls 
'* Microsoft HTML Object Library 

Sub Torrent_Data() 
    Dim row As Long 
    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim post As Object 

    With IE 
     .Visible = True 
     .navigate "https://yts.am/browse-movies" 
     Do While .readyState <> READYSTATE_COMPLETE: 
      DoEvents 
     Loop 
     Set html = .document 
    End With 

    Dim dicFilms As Scripting.Dictionary 
    Set dicFilms = New Scripting.Dictionary 

    Call GetScriptEngine.Run("getMovies", html, dicFilms) 

    Dim vFilms As Variant 
    vFilms = dicFilms.Keys 

    Dim vYears As Variant 
    vYears = dicFilms.Items 

    Dim lRowLoop As Long 
    For lRowLoop = 0 To dicFilms.Count - 1 

     Cells(lRowLoop + 1, 1) = vFilms(lRowLoop) 
     Cells(lRowLoop + 1, 2) = vYears(lRowLoop) 

    Next lRowLoop 

    Stop 

    IE.Quit 
End Sub 

Private Function GetScriptEngine() As ScriptControl 
    '* see code from this SO Q & A 
    ' https://stackoverflow.com/questions/37711073/in-excel-vba-on-windows-how-to-get-stringified-json-respresentation-instead-of 
    Static soScriptEngine As ScriptControl 
    If soScriptEngine Is Nothing Then 
     Set soScriptEngine = New ScriptControl 
     soScriptEngine.Language = "JScript" 

     soScriptEngine.AddCode "function getMovies(htmlDocument, microsoftDict) { " & _ 
            "var titles = htmlDocument.querySelectorAll('a.browse-movie-title'), i;" & _ 
            "var years = htmlDocument.querySelectorAll('div.browse-movie-year'), j;" & _ 
            "if (years.length === years.length) {" & _ 
            "for (i=0; i< years.length; ++i) {" & _ 
            " var film = titles[i].innerText;" & _ 
            " var year = years[i].innerText;" & _ 
            " microsoftDict.Add(film, year);" & _ 
            "}}}" 

    End If 
    Set GetScriptEngine = soScriptEngine 
End Function 
0

好吧,看來我找到了解決方案,與.queryselectorAll()一起工作。經過多次試驗後,我可以注意到它只與for loop有一些問題,所以我巧妙地避免了for loop,而是用with block來完成同樣的工作。以下是我們如何做到這一點:

Sub Torrent_Data() 

    With CreateObject("InternetExplorer.Application") 
     .Visible = False 
     .navigate "https://yts.am/browse-movies" 
     While .Busy = True Or .readyState < 4: DoEvents: Wend 

     With .document.querySelectorAll(".browse-movie-bottom") 
      For I = 0 To .Length - 1 
       Cells(I + 1, 1) = .Item(I).querySelector(".browse-movie-title").innerText 
       Cells(I + 1, 2) = .Item(I).querySelector(".browse-movie-year").innerText 
      Next I 
     End With 
    End With 

End Sub 

順便說一句,上述腳本可以在不引用任何內容的情況下執行。

相關問題