2017-12-27 128 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 

顺便说一句,上述脚本可以在不引用任何内容的情况下执行。

相关问题