I have looked at the solution provided in this link Extract Table from Webpage in Excel using VBA and it was very helpful. But I need to extract the elements with certain classes in each HTML Table cells(td).
The URL is: https://www.betfair.com/exchange/plus/football/competition/11997260
The HTML table class is: coupon-table
The price to extract is nested inside cells. It is in the Span element using the "bet-button-price" class. That's the data I need extracted into each cell in the Excel sheet.
Here's a screenshot of the table structure:
I would be grateful to receive any help to extract those prices in each cell into the sheet.
Public Sub GetInfo()
Const URL As String = "https://www.betfair.com/exchange/plus/football/competition/11997260"
Dim html As HTMLDocument, hTable As HTMLTable, ws As Worksheet, headers()
headers = Array("Countries", "Prices")
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
html.body.innerHTML = .responseText
End With
Set hTable = html.querySelector("table.coupon-table")
Dim Td As Object, Tr As Object, r As Long, c As Long
r = 1
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
For Each Tr In hTable.getElementsByTagName("tr")
r = r + 1: c = 1
If r > 3 Then
For Each Td In Tr.getElementsByClassName("bet-button-price")
.Cells(r - 2, c) = IIf(c = 2, "'" & Td.innerText, Td.innerText)
c = c + 1
Next
End If
Next
End With
End Sub