Excel VBA web scraping for data table

前端 未结 2 1368
臣服心动
臣服心动 2020-12-20 10:51

I am trying to get data from Roster Resource, here\'s an example of a webpage (https://www.rosterresource.com/mlb-arizona-diamondbacks). At the very minimum, I want to get t

2条回答
  •  梦毁少年i
    2020-12-20 11:04

    If you navigate the webpage https://www.rosterresource.com/mlb-arizona-diamondbacks and choose Inspect element from context menu on the table, you will see in browser developer tools that the whole table is located within a frame:

    
    

    So actually you need to retrieve the data from that Google Spreadsheet document. That could be done with XHR and Regex, as shown in the below code:

    Option Explicit
    
    Sub Test()
    
        Dim sContent As String
        Dim i As Long
        Dim j As Long
        Dim k As Long
        Dim aTables()
        Dim aHeader() As String
        Dim aRows() As String
    
        ' Retrieve HTML content via XHR
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "https://www.rosterresource.com/mlb-arizona-diamondbacks", False
            .Send
            sContent = .ResponseText
        End With
        ' Cut all before iframe URL
        sContent = Split(sContent, "