首页 > 解决方案 > 使用“分页”更改网站页面的宏

问题描述

我正在尝试在 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

标签: excelvbainternet-explorerweb-scraping

解决方案


该页面对我来说无法正确加载,但是在您找到页面数并单击>按钮直到用尽所有页面之前,类似以下内容的内容应该可以工作。

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

推荐阅读