使用excel VBA

时间:2017-07-09 08:59:28

标签: xml excel-vba vba excel

我想从网站下载表格数据:http://stats.nba.com/teams/four-factors/#!?sort=TEAM_NAME&dir=1

  1. 我尝试使用excel功能:数据>来自网络 - 它没有用。

  2. 我使用了以下代码,但没有返回任何内容。 - 但它正在与其他网站合作。

  3. 当我们使用"查看来源" 查看网页时,表数据不可用,但如果我们使用" Inspect Element" 表数据可用。

    那么,有没有办法在网页上查看检查元素并使用Excel VBA废弃数据?

        Sub UsingXML()
    
        Dim XMLPage As New MSXML2.XMLHTTP60
        Dim HTMLDoc As New MSHTML.HTMLDocument
    
        XMLPage.Open "GET", "http://stats.nba.com/teams/four-factors/#!?sort=TEAM_NAME&dir=1", False
        XMLPage.send
    
        HTMLDoc.body.innerHTML = XMLPage.responseText
    
        ProcessHTMLPage HTMLDoc
    
        End Sub
    
        Sub ProcessHTMLPage(HTMLPage As MSHTML.HTMLDocument)
    
        Dim HTMLTable As MSHTML.IHTMLElement
        Dim HTMLTables As MSHTML.IHTMLElementCollection
        Dim HTMLRow As MSHTML.IHTMLElement
        Dim HTMLCell As MSHTML.IHTMLElement
        Dim RowNum As Long, ColNum As Integer
    
        Set HTMLTables = HTMLPage.getElementsByTagName("table")
    
        For Each HTMLTable In HTMLTables
        Worksheets.Add
        Range("A1").Value = HTMLTable.className
        Range("B1").Value = Now
    
        RowNum = 2
        For Each HTMLRow In HTMLTable.getElementsByTagName("tr")
    
            ColNum = 1
            For Each HTMLCell In HTMLRow.Children
                Cells(RowNum, ColNum) = HTMLCell.innerText
                ColNum = ColNum + 1
            Next HTMLCell
    
            RowNum = RowNum + 1
        Next HTMLRow
    
        Next HTMLTable
    
        End Sub
    

    请告知您此代码中缺少的内容。谢谢!

0 个答案:

没有答案