代码不会将第一个网页后的数据提取到Excel中

时间:2017-07-27 16:55:04

标签: excel excel-vba web-scraping vba

我有以下代码,可以从网站首页上的表中获取数据(价格,名称,货币,更改等)

Public Sub GetTeamData()
Dim strWebAddress As String
Dim strH2AnchorContent As String
Dim IEDocument As MSHTML.HTMLDocument
Dim objH2 As MSHTML.HTMLHeaderElement
Dim obTable As MSHTML.HTMLTable
Dim objRow As MSHTML.HTMLTableRow
Dim objCell As MSHTML.HTMLTableCell
Dim lngRow As Long
Dim lngColumn As Long

' initialize some variables that should probably better be passed as paramaters or defined as constants
strWebAddress = "https://toolkit.financialexpress.net/santanderam"

dateNow = Now
bExitLoop = False
lngTimeoutInSeconds = 5
Do While Not bExitLoop
  If Now > DateAdd("s", lngTimeoutInSeconds, dateNow) Then Exit Do
Loop

' open page
Set IEDocument = GetIEDocument(strWebAddress)
If IEDocument Is Nothing Then
    MsgBox "Timeout reached opening this address:" & vbNewLine & strWebAddress, vbCritical
    Exit Sub
End If

    Dim ButtonData As Variant
    Set ButtonData = IEDocument.getElementsByClassName("paginator fe-paging-navContainer")

Dim button As MSHTML.HTMLLinkElement
For Each button In ButtonData

   Debug.Print button.nodeName

        button.Click

          ' retrieve anchor element
          Set oTable = IEDocument.getElementById("Price_1_1")
             Debug.Print oTable.innerText

              ' iterate over the table and output its contents
              lngRow = 1
              For Each objRow In oTable.Rows
              lngColumn = 1
              For Each objCell In objRow.Cells
                  Cells(lngRow, lngColumn) = objCell.innerText
                  lngColumn = lngColumn + 1
              Next objCell
              lngRow = lngRow + 1
           Next
         Next button
End Sub

我的问题是我无法从下一页(1..7)获取数据。

任何人都可以帮助解释为什么以上内容不会从下一页中提取数据? 谢谢!

1 个答案:

答案 0 :(得分:0)

打开页面的代码后,用下面的代码替换其余的代码。您可能需要稍微调整一下,但它应该遍历所有可用页面:

' Set the object for 'Next' button
Dim oNext As Variant
Set oNext = IEDocument.getElementsByClassName("ui-paging-button ui-state-default ui-corner-all ui-paging-next")

' Loop to go through all pages
Dim bExitMLoop As Boolean: bExitMLoop = False
lngRow = 1
Do While Not bExitMLoop

    ' Get data from current page
    Set oTable = IEDocument.getElementById("Price_1_1")
    For Each objRow In oTable.Rows
        lngColumn = 1
        For Each objCell In objRow.Cells
            Cells(lngRow, lngColumn) = objCell.innerText
            lngColumn = lngColumn + 1
        Next objCell
        lngRow = lngRow + 1
    Next

    ' Check if Next button is available
    If oNext.Length = 0 Then
        bExitMLoop = True
    Else
        oNext.Item(0).Click

        ' Wait for page to refresh (could check the ready state here as well)
        dateNow = Now
        bExitLoop = False
        lngTimeoutInSeconds = 3
        Do While Not bExitLoop
            If Now > DateAdd("s", lngTimeoutInSeconds, dateNow) Then Exit Do
        Loop

        ' Reset 'Next' button object
        Set oNext = Nothing
        Set oNext = IEDocument.getElementsByClassName("ui-paging-button ui-state-default ui-corner-all ui-paging-next")
    End If

Loop