1

I am trying to extract the details in this webpage and they seem to be under certain "divs" with "selection-left" and "selection-right" right. I haven't found a way to successfully pull it yet. This is the URL - https://sports.ladbrokes.com/en-af/betting/golf/golf-all-golf/us-masters/2020-us-masters/228648232/

And here is an image of what I want to extract. I want to copy the competition name and each participant and score. enter image description here

I have tried using QHar's approach in this link - How to extract values from nested divs using VBA. But I'm getting errors along this line - ReDim results(1 To countries.Length / 2, 1 To 4)

Here is the code I've been trying to make work

Option Explicit

Public Sub GetData()
Dim html As HTMLDocument, ws As Worksheet, countries As Object, scores As Object, results(), i As 
Long, r As Long

Set ws = ThisWorkbook.Worksheets("Sheet1"): Set html = New HTMLDocument: r = 1

With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://sports.ladbrokes.com/en-af/betting/golf/golf-all-golf/us-masters/2020-us-masters/228648232/", False
    .send
    html.body.innerHTML = .responseText
End With

Set participant = html.querySelectorAll(".market-content .selection-left"): Set scores = html.querySelectorAll("..market-content .selection-right")
ReDim results(1 To countries.Length / 2, 1 To 4)

For i = 0 To participant.Length - 1 Step 2
    results(r, 1) = participant.item(i).innerText: results(r, 2) = "'" & scores.item(i).innerText

    r = r + 1
Next
ws.Cells(1, 1).Resize(1, 4) = Array("Competition", "Participant", "Score")
ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End Sub

I will need help to make this code work

QHarr
  • 80,579
  • 10
  • 51
  • 94
Smith O.
  • 187
  • 2
  • 11
  • What if `countries.Length` is not an even number? Or zero? "I'm getting errors" is not a very useful description of what you see when running your code - what's the exact error message? – Tim Williams Sep 20 '19 at 05:55
  • It seems that you declare variables "countries", but you do not set its value so it is equal to nothing when you try to use its length property. – EddiGordo Sep 20 '19 at 06:20
  • Your best bet here would be selenium basic as content is rendered dynamically. You could try mimicking the POST request the page makes but it is not simple. – QHarr Sep 20 '19 at 06:34

1 Answers1

0

Content is dynamically added so will not be present in your current request format; hence your error as you have a nodeList of Length 0. You could try making POST requests as the page does but it doesn't look like a quick and easy bit of coding. I would go with browser automation, if this is a small project, so that js can run on the page and you can click the show more button. You will need a wait condition for the page to have properly loaded. I use the presence of the show more button.

Option Explicit

Public Sub GetOddsIE()
    Dim d As InternetExplorer, odds As Object, names As Object, i As Long
    Dim ws As Worksheet, results(), competition As String

    Set d = New InternetExplorer
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Const URL = "https://sports.ladbrokes.com/en-af/betting/golf/golf-all-golf/us-masters/2020-us-masters/228648232/"

    With d
        .Visible = False
        .Navigate2 URL
        While .Busy Or .ReadyState <> 4: DoEvents: Wend
        With .Document.getElementsByClassName("expandable-below-container-button")
            Do
                DoEvents
            Loop While .Length = 0  'wait for element to be present
            .Item(0).Click 'click on show more
        End With

        Set names = .Document.getElementsByClassName("selection-left-selection-name")
        Set odds = .Document.getElementsByClassName("odds-convert")
        competition = .Document.getElementsByClassName("league")(0).innerText

        ReDim results(1 To names.Length, 1 To 3)

        For i = 0 To names.Length - 1
            results(i + 1, 1) = competition
            results(i + 1, 2) = names.Item(i).innerText
            results(i + 1, 3) = "'" & odds.Item(i).innerText
        Next
        .Quit
    End With
    ws.Cells(1, 1).Resize(1, 3) = Array("Competition", "Participant", "Score")
    ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End Sub

QHarr
  • 80,579
  • 10
  • 51
  • 94