宏以“分页”更改网站中的页面

时间:2019-05-26 14:50:24

标签: excel vba internet-explorer web-scraping

我正在尝试在Excel中编程宏以更改以下网站中的页面:https://cebra.com.ar/category/18/Disfraces-and-Accesorios.html

该页面没有可单击的典型按钮,因此,该页面上存在的几种解决方案无法正常工作...

下面是我编写的代码,效果很好,只是缺少最终的点击。

有人可以帮我开发这个宏吗? 提前非常感谢您!

Sub change_webpage()

    Dim ie As InternetExplorer

    Dim lis As IHTMLElementCollection

    Dim nextLi As HTMLLIElement, i As Long

    sheetnom = ActiveSheet.Name

    link = "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

    Worksheets(sheetnom).Activate

    Set ie = New InternetExplorer

    ie.Visible = True

    ie.navigate link

    Do While ie.readyState <> READYSTATE_COMPLETE

        Application.StatusBar = "Loading Web page …"

        DoEvents

    Loop

    newNum = -1

    Set objIE = New InternetExplorer

    objIE.navigate fullUrl

    Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

    Set currPage = ie.document

    Do Until oldNum = newNum

        oldNum = newNum

        newNum = currPage.getElementsByClassName("box-data").length

        Application.Wait Now + TimeSerial(0, 0, 2)

        currPage.parentWindow.scrollBy 0, 100000

        Application.Wait Now + TimeSerial(0, 0, 2)

        If newNum > 400 Then newNum = 400

    Loop

    Set lis = ie.document.getElementsByClassName("pagination")

    Set nextLi = Nothing

    i = 0

    While i < lis.length And nextLi Is Nothing

        If lis(i).innerText = "2" Then Set nextLi = lis(i)

        i = i + 1

    Wend

    If Not nextLi Is Nothing Then

        nextLi.Change = Active

    End If

    'ie.Quit

    link = Empty

    Sheets(sheetnom).Select

    Range("A1").Select

    MsgBox "Done"

End Sub

1 个答案:

答案 0 :(得分:0)

该页面无法为我正确加载,但是在您找到页面数并单击> 按钮,直到用尽所有页面之前,应执行以下操作。

Option Explicit
'VBE > Tools > References: Microsoft Internet Controls

Public Sub ClickThroughPages()
    Dim ie As New InternetExplorer, numberOfPages As Long, page As Long
    With ie
        .Visible = True
        .Navigate2 "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            numberOfPages = .querySelectorAll(".setPage").length

            'do something with page 1
            If numberOfPages > 1 Then
                For page = 2 To numberOfPages

                    .querySelector(".fa-angle-right").Click

                    While ie.Busy Or ie.readyState < 4: DoEvents: Wend

                    'do something with next pages
                Next
            End If
        End With
        Stop
        .Quit
    End With