Can't use querySelector in the right way in vba

前端 未结 4 775
醉酒成梦
醉酒成梦 2021-01-14 21:48

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 wel

4条回答
  •  再見小時候
    2021-01-14 22:24

    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
    

    This is another way which doesn't need error handler:

    Sub GetContent()
        Const URL$ = "https://yify-torrent.cc/search/1080p/"
        Dim HTMLDoc As New HTMLDocument, R&, I&
    
        With New ServerXMLHTTP60
            .Open "Get", URL, False
            .send
            HTMLDoc.body.innerHTML = .responseText
        End With
    
        With HTMLDoc.querySelectorAll("h3 > a.movielink")
            For I = 0 To .Length - 1
                R = R + 1: Cells(R, 1).Value = .Item(I).innerText
            Next I
        End With
    End Sub
    

提交回复
热议问题