【问题标题】:How to click on a certain page within a web using "pagination"?如何使用“分页”点击网页中的某个页面?
【发布时间】:2019-10-19 13:54:11
【问题描述】:

我有一个程序可以打开一个网页并计算其中有多少页。然后我需要点击某个页面,例如第 3 号。有人知道怎么做吗?我在下面详细介绍了宏的代码。非常感谢!

Sub test()

    Dim element As IHTMLElement
    Dim elements As IHTMLElementCollection
    Dim ie As InternetExplorer
    Dim numberOfPages As Double
    Dim html As HTMLDocument

    Set ie = New InternetExplorer
    ie.Visible = True
    ie.navigate "https://cebra.com.ar/category/73/Juego-de-Construccion.html"
    Do While ie.readyState <> READYSTATE_COMPLETE
        Application.StatusBar = "Loading Web page …"
        DoEvents
    Loop
    Set html = ie.document
    Set elements = html.getElementsByClassName("container")
    Set ElementCol = html.getElementsByTagName("a")
    numberOfPages = ie.document.querySelectorAll(".setPage").Length
    'Here I want to click on a certain page, for example, number 3
    For Each ele In ie.document.getElementsByTagName("li")
        For Each element In elements
            If element.className = "container" Then
                'Do something
            End If
        Next element
    Next
    MsgBox "Done"

End Sub

【问题讨论】:

    标签: excel vba web web-scraping pagination


    【解决方案1】:

    试试下面的代码

    Option Explicit
    
    Sub test()
    
        Dim ie As InternetExplorer
        Dim html As HTMLDocument
        Dim element As IHTMLElement
    
        Set ie = New InternetExplorer
        ie.Visible = True
        ie.Navigate "https://cebra.com.ar/category/73/Juego-de-Construccion.html"
        WaitIE ie
        Set html = ie.Document
        Set element = html.querySelector("a.setPage")
        element.setAttribute "data-value", "3" ' set the page number you want open
        element.click
        WaitIE ie
    
        MsgBox "Done"
    
    End Sub
    
    Sub WaitIE(ie As InternetExplorer)
    
        Application.StatusBar = "Loading Web page …"
        Do While ie.readyState <> READYSTATE_COMPLETE
            DoEvents
        Loop
        Application.StatusBar = "Ready"
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      假设您想要循环所有页面(但也显示如何选择给定页面),您需要检查当前活动页面是什么,然后循环其他页面。需要等待条件以及错误处理,以避免陈旧的元素异常因权限被拒绝和未找到元素而冒泡。


      说明:

      这个

      currentPage = CLng(.querySelector(".active").innerText)
      

      查找当前活动页面。当前活动页面有一个值为active 的类属性。 . 是一个 CSS class selector。

      这个

      If page <> currentPage Then
      

      在所有页面的循环中忽略当前活动页面。

      这个

      cssSelector = ".setPage[data-value='" & page & "']"
      

      通过将页码连接到data-value 属性来定义选择器以选择任何给定的页面。它与setPage 的类选择器. 结合使用,以限制页面选择的相关元素:

      [data-value='pageNumber']、pageNumber 是示例页面,例如2、是一个cssattribute = value selector。

      如果您检查下面的 html:

      您可以看到,如果当前活动页面为 1,则有一个类属性值 = active 并且没有 data-value 属性。要选择第二页,您可以看到当前的类值是setPage,而data-value 是2。

      这个

      .querySelector(cssSelector).Click
      

      在循环中选择下一页。

      这个

      Do
          On Error Resume Next
          num = CLng(.querySelector(".active").innerText)
          On Error GoTo 0
      Loop Until num = page
      

      循环,直到所选页码的 class 属性值处于活动状态,并且其 innerText 值 = 到所需的页面,即当 num = 2 时选择第 2 页,以便 html 更改为:

      您可以在不同条件下以多种方式重新编写此循环,但这个工作正常。


      VBA:

      Option Explicit
      
      'VBE > Tools > References: Microsoft Internet Controls
      Public Sub GetData()
          Dim ie As Object, numberOfPages As Long, currentPage As Long, page As Long
          Set ie = CreateObject("InternetExplorer.Application")
          With ie
              .Visible = True
              .Navigate2 "https://cebra.com.ar/category/73/Juego-de-Construccion.html"
      
              While .Busy Or .readyState < 4: DoEvents: Wend
      
              With .document
                  numberOfPages = .querySelectorAll(".setPage").Length
                  currentPage = CLng(.querySelector(".active").innerText)
                  Dim cssSelector As String, num As Long
                  For page = 1 To numberOfPages
                      If page <> currentPage Then
                          cssSelector = ".setPage[data-value='" & page & "']"
                          .querySelector(cssSelector).Click
                          Do
                              On Error Resume Next
                              num = CLng(.querySelector(".active").innerText)
                              On Error GoTo 0
                          Loop Until num = page
                          While ie.Busy Or ie.readyState < 4: DoEvents: Wend
                      End If
                  Next
              End With
              .Quit
          End With
      End Sub
      

      【讨论】:

      • 你试过这些答案了吗?
      猜你喜欢
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多