Web-scraping across multipages without even knowing the last page number

后端 未结 1 387
梦如初夏
梦如初夏 2021-01-15 18:45

Running my code for a site to crawl the titles of different tutorials spreading across several pages, I found it working flawless. I tried to write some code not depending o

相关标签:
1条回答
  • 2021-01-15 19:29

    You can always rely on elements if they exits or not. Here for example, if you try to use the object which you have set your element to, you will get:

    Run-time error '91': Object variable or With block variable not set

    This is the key you should be looking for to put an end to your code. Please see the below example:

    Sub yify()
    Const mlink = "https://www.yify-torrent.org/genres/western/p-"
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim post As Object
    Dim posts As Object
    
    y = 1
    Do
        With http
            .Open "GET", mlink & y & "/", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Set posts = html.getElementsByClassName("mv")
        On Error GoTo Endofpage
        Debug.Print Len(posts) 'to force Error 91
    
        For Each post In posts
            With post.getElementsByTagName("div")
                x = x + 1
                If .Length Then Cells(x, 1) = .Item(0).innerText
            End With
        Next post
        y = y + 1
    Endofpage:
    Loop Until Err.Number = 91
    Debug.Print "It's over"
    End Sub
    
    0 讨论(0)
提交回复
热议问题