Anuj
Anuj

Reputation: 107

How to scrape data from Datastore.prime using vba

I am trying to scrape data from this website http://www.whoscored.com/regions/252/tournaments/2/england-premier-league

When I use inspect element I see that data is in table format as shown in picture below.

enter image description here

The source code has the data in this format.

enter image description here

DataStore.prime('stagefixtures', $.extend({ stageId: 12496, isAggregate: false }, calendar.parameter()), [[959688,1,'Monday, Dec 21 2015','20:00',13,'Arsenal',0,167,'Manchester City',0,'2 : 1','2 : 0',1,1,'FT','1',0,1,112,0] ,[959683,4,'Saturday, Dec 26 2015','12:45',96,'Stoke',0,32,'Manchester United',0,'vs',,0,1,,'-1',0,1,13,0] ,[959615,4,'Saturday, Dec 26 2015','15:00',24,'Aston Villa',0,29,'West Ham',0,'vs',,0,1,,'-1',0,1,6,0] ,[959625,4,'Saturday, Dec 26 2015','15:00',183,'Bournemouth',0,162,'Crystal Palace',0,'vs',,0,1,,'-1',0,1,10,0] ,[959635,4,'Saturday, Dec 26 2015','15:00',15,'Chelsea',0,27,'Watford',0,'vs',,0,1,,'-1',0,1,15,0] ,[959645,4,'Saturday, Dec 26 2015','15:00',26,'Liverpool',0,14,'Leicester',0,'vs',,0,1,,'-1',0,1,15,0] ,[959655,4,'Saturday, Dec 26 2015','15:00',167,'Manchester City',0,16,'Sunderland',0,'vs',,0,1,,'-1',0,1,4,0] ,[959691,4,'Saturday, Dec 26 2015','15:00',259,'Swansea',0,175,'West Bromwich Albion',0,'vs',,0,1,,'-1',0,1,5,0] ,[959698,4,'Saturday, Dec 26 2015','15:00',30,'Tottenham',0,168,'Norwich',0,'vs',,0,1,,'-1',0,1,8,0] ,[959665,4,'Saturday, Dec 26 2015','17:30',23,'Newcastle United',0,31,'Everton',0,'vs',,0,1,,'-1',0,1,7,0] ,[959674,4,'Saturday, Dec 26 2015','19:45',18,'Southampton',0,13,'Arsenal',0,'vs',,0,1,,'-1',0,1,11,0] ]);

This code is supposed to scrape data from the table format but I don't how to do it in this case.

Option Explicit

Sub WeeklyFixtures()


 Dim IE As Object, obj As Object

 Dim r As Integer, c As Integer, t As Integer
 Dim elemCollection As Object

 Set IE = CreateObject("InternetExplorer.Application")

 With IE
 .Visible = True
 .navigate ("http://www.whoscored.com/regions/252/tournaments/2/england-premier-league")
While IE.ReadyState <> 4
DoEvents
Wend

 Do While IE.busy: DoEvents: Loop

ThisWorkbook.Sheet1.Clear

 Set elemCollection = IE.Document.getElementsByTagName("TABLE")

    For t = 0 To (elemCollection.Length - 1)

        For r = 0 To (elemCollection(t).Rows.Length - 1)
            For c = 0 To (elemCollection(t).Rows(r).Cells.Length - 1)
                ThisWorkbook.Worksheets(1).Cells(r + 1, c + 1) = elemCollection(t).Rows(r).Cells(c).innerText
            Next c
        Next r
    Next t

 End With

 Set IE = Nothing

 End Sub

Upvotes: 2

Views: 528

Answers (2)

omegastripes
omegastripes

Reputation: 12612

Try this code:

Option Explicit

Sub GetWhoscoredData()

    Dim strCont, arrRows, strComma, arrQuots, i, arrCols

    With CreateObject("Microsoft.XMLHTTP")
        .Open "GET", "http://www.whoscored.com/regions/252/tournaments/2/england-premier-league", False
        .Send
        strCont = .ResponseText
    End With

    strCont = Split(strCont, "'stagefixtures'")(1)
    strCont = Split(strCont, "[[")(1)
    strCont = Split(strCont, "]);")(0)
    strCont = Replace(strCont, vbCrLf, "")
    strComma = Mid(CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
    arrQuots = Split(strCont, "'")
    For i = 1 To UBound(arrQuots) Step 2
        arrQuots(i) = Replace(arrQuots(i), ",", strComma)
    Next
    strCont = Join(arrQuots, "")
    arrRows = Split(strCont, "],[")
    For i = 0 To UBound(arrRows)
        arrCols = Split(arrRows(i), ",")
        Cells(i + 1, 1).Value = Replace(arrCols(2), strComma, ",")
        Cells(i + 1, 2).Value = Replace(arrCols(3), strComma, ",")
        Cells(i + 1, 3).Value = Replace(arrCols(14), strComma, ",")
        Cells(i + 1, 4).Value = Replace(arrCols(5), strComma, ",")
        Cells(i + 1, 5).NumberFormat = "@"
        Cells(i + 1, 5).Value = Replace(arrCols(10), strComma, ",")
        Cells(i + 1, 6).Value = Replace(arrCols(8), strComma, ",")
    Next
    Cells.Columns.AutoFit

End Sub

The output it gives for me is as follows:

output

Upvotes: 1

Ralph
Ralph

Reputation: 9444

Do you have Excel 2016? There you should be able to get it with: Data --> New Query --> From Other Sources --> From Web. There you can enter your URL. If you record all of that you get even the corresponding VBA code.

enter image description here

Upvotes: 1

Related Questions