无法在 vba 中以正确的方式使用 querySelector

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45781861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 13:08:00  来源:igfitidea点击:

Can't use querySelector in a proper way in vba

vbaweb-scraping

提问by SIM

I've written some code using vba to get all the movie names from a specific webpage out of a torrent site. However, pressing "F8" I could find out that the code works well and prints the results until it hits the last result from that page. As soon as it reaches the last name to parse, the program crashes. I did several times and suffered the same consequences. If vba doesn't support this css selector method then how could I collect results before the last one? Is there any reference to add in the library or something else before execution? Any help on this will be vastly appreciated.

我已经使用 vba 编写了一些代码,以从洪流站点的特定网页中获取所有电影名称。但是,按“F8”我可以发现代码运行良好并打印结果,直到它到达该页面的最后一个结果。一旦到达要解析的姓氏,程序就会崩溃。我做了几次并遭受了同样的后果。如果 vba 不支持这种 css 选择器方法,那么我如何在最后一个之前收集结果?在执行之前是否有任何引用添加到库中或其他内容中?对此的任何帮助将不胜感激。

Here is the code I have written:

这是我写的代码:

Sub Torrent_data()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim movie_name As Object, movie As Object

    With http
        .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
        .send
        html.body.innerHTML = .responseText
    End With

    Set movie_name = html.querySelectorAll("div.mv h3 a")

    For Each movie In movie_name
        x = x + 1: Cells(x, 1) = movie.innerText
    Next movie

End Sub

回答by Tehscript

Try this:

尝试这个:

Sub Torrent_data()

    Dim http As New XMLHTTP60, html As New HTMLDocument, x As Long

    With http
        .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
        .send
        html.body.innerHTML = .responseText
    End With

    Do
    x = x + 1
    On Error Resume Next
    Cells(x, 1) = html.querySelectorAll("div.mv h3 a")(x - 1).innerText
    Loop Until Err.Number = 91

End Sub

回答by jsotola

the code retrieves one element after the last movie

该代码在最后一部电影之后检索一个元素

this extra element causes the failure, so for each ...cannot be used

这个额外的元素会导致失败,所以for each ...不能使用

not sure why ... yet .... will update

不知道为什么......然而......会更新

Sub Torrent_data()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim movie_name As Object, movie As Object

    With http
        .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
        .send
        html.body.innerHTML = .responseText
    End With

    Set movie_name = html.querySelectorAll("div.mv h3 a")

    Dim i As Integer
    For i = 0 To movie_name.Length - 1
        Cells(x + i, 1) = movie_name(i).innerText
    Next i

End Sub

回答by jsotola

looks like querySelectorAllhas an issue of some sort

看起来querySelectorAll有某种问题

the object html.querySelectorAll(".mv h3 a")cannot be examined in Watch window.

html.querySelectorAll(".mv h3 a")无法在监视窗口中检查对象。

attempting to do so crashes excel or word (i tried both)

尝试这样做会导致 excel 或 word 崩溃(我都试过)

tried other tags, same result

尝试了其他标签,结果相同

Sub Torrent_data()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim movie_name As Object, movie As Object

    With http
        .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
        .send
        html.body.innerHTML = .responseText
    End With

'   Set movie_name = html.querySelectorAll("div.mv h3 a")   ' querySelectorAll crashes VBA when trying to examine movie_name object

    Set movie_name = html.getElementsByClassName("mv")      ' HTMLElementCollection

    For Each movie In movie_name
        x = x + 1: Cells(x, 1) = movie.getElementsByTagName("a")(1).innerText
    Next movie

'   HTML block for each movie looks like this

'   <div class="mv">
'       <h3>
'           <a href='/movie/55346/download-smoke-1995-1080p-mp4-yify-torrent.html' target="_blank" title="Smoke (1995) 1080p">Smoke (1995) 1080p</a>
'       </h3>
'       <div class="movie">
'           <div class="movie-image">
'               <a href="/movie/55346/download-smoke-1995-1080p-mp4-yify-torrent.html" target="_blank" title="Download Smoke (1995) 1080p">
'                   <span class="play"><span class="name">Smoke (1995) 1080p</span></span>
'                   <img src="//pic.yify-torrent.org/20170820/55346/smoke-1995-1080p-poster.jpg" alt="Smoke (1995) 1080p" />
'               </a>
'           </div>
'       </div>
'       <div class="mdif">
'           <ul>
'               <li><b>Genre:</b>Comedy</li><li><b>Quality:</b>1080p</li><li><b>Screen:</b>1920x1040</li><li><b>Size:</b>2.14G</li><li><b>Rating:</b>7.4/10</li><li><b>Peers:</b>2</li><li><b>Seeds:</b>0</li>
'           </ul>
'           <a href="/movie/55346/download-smoke-1995-1080p-mp4-yify-torrent.html" class="small button orange" target="_blank" title="Download Smoke (1995) 1080p YIFY Torrent">Download</a>
'       </div>
'   </div>

End Sub

回答by davinceleecode

I know this old, but I managed on how to use querySelectorAll without crashes my IE.

我知道这很旧,但我设法使用 querySelectorAll 而不会使我的 IE 崩溃。

Instead of using For-eachI used For Loop

而不是使用For-each我使用For Loop

Example below:

下面的例子:

Dim priceData as Object
Set priceData = IE.document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']")


For i = 0 to priceData.Length - 1
    Debug.Print priceData.item(i).getElementsByClassName("cash js_linkInsideCell")(0).innerHTML
Next i