VBA website scraping - multiple link clicks to show full table - HELP

  • Hi,

    I would like to request some help in relation to the coding below. I am trying to load up the full table of the website indicated in the code, by clicking multiple times (>20 X) the text link "Show more matches".
    As soon as the full table is loaded in the IE i will "dump it to the worksheet.
    My issue is to re-iterate the "Show more matches" link in the bottom of the page.

    Thank you for your help in advance.

    Sub TestResults()

    Dim IE As Object
    Dim i As Long
    Dim strText As String
    Dim jj As Long
    Dim hBody As Object
    Dim hTR As Object
    Dim hTD As Object
    Dim tb As Object
    Dim bb As Object
    Dim Tr As Object
    Dim Td As Object
    Dim ii As Long
    Dim doc As Object
    Dim hTable As Object
    Dim y As Long
    Dim z As Long
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim hyper_link As Object


    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    Set wb = Excel.ActiveWorkbook
    Set ws = wb.ActiveSheet

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    y = 1 'Column A in Excel
    z = 1 'Row in Excel

    'Navigate in Internet Explorer

    IE.navigate "https://www.flashscore.com/baseball/usa/mlb/results/"

    Do While IE.Busy: DoEvents: Loop
    Do While IE.readyState <> 4: DoEvents: Loop

    'Show all table elements
    Set AllHyperlinks = Nothing
    Set AllHyperlinks = IE.document.getElementsByTagName("a")

    For Each hyper_link In AllHyperlinks

    If hyper_link.innerText = "Show more matches" Then


    Do While IE.Busy: DoEvents: Loop
    'Do While IE.readyState <> 4: DoEvents: Loop

    Application.Wait (Now + TimeValue("00:00:02"))

    Exit For

    End If


    Loop While IE.readyState <> 4 'hyper_link.innerText = "Show more matches"

    'Collecting table from web and paste it to the excel sheet

    Set doc = IE.document
    Set hTable = doc.getElementsByTagName("table")

    For Each tb In hTable
    Set hBody = tb.getElementsByTagName("tbody")
    For Each bb In hBody
    Set hTR = bb.getElementsByTagName("tr")
    For Each Tr In hTR
    Set hTD = Tr.getElementsByTagName("td")
    y = 1 ' Resets back to column A
    For Each Td In hTD
    ws.Cells(z, y).Value = Td.innerText
    y = y + 1
    Next Td
    z = z + 1
    Next Tr
    Exit For
    Next bb
    Exit For
    Next tb

    Set IE = Nothing
    Set doc = Nothing
    Set hTable = Nothing
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True

    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!