excelweb-scrapinghtml-tableextractvba

VBA web scraping


I am trying to get a row of data from this table on this website: http://www.nasdaq.com/symbol/neog/financials?query=balance-sheet

Now I can manage to get the "total liabilities" row using the

doc.getelementsbyclassname("net")(3).innertext 

but I cannot figure out how to get any other rows of data such as common stock.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = Range("bscode").Row And _
Target.Column = Range("bscode").Column Then
Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://www.nasdaq.com/symbol/" & Range("bscode").Value & "/financials?query=balance-sheet&data=quarterly"

Do
 DoEvents

Loop Until IE.readyState = READYSTATE_COMPLETE

Dim Doc As HTMLDocument
Set Doc = IE.document
Dim sD As String
sD = Doc.getElementsByTagName("tr")(8).innerText

MsgBox sD

Dim aD As Variant
aD = Split(sD, "$")
Range("bs").Value = aD(1)
Range("ba").Value = aD(2)
Range("bb").Value = aD(3)
Range("bc").Value = aD(4)

End If
End Sub

If it helps, I have the HTML source and the tr highlighted that I want to grab.

screenshot of HTML code

The issue is the method of finding the table row data. Could someone please explain to me how to get other rows of data? It would be much appreciated !


Solution

  • I was able to do some trial and error and to get the correct reference this way:

    Dim eTR As Object, cTR As Object, I as Integer 'I used object, because I did late binding
    Set cTR = Doc.getElementsByTagName("tr")
    
    i = 0
    
    For Each eTR In cTR
    
        If Left(eTR.innerText, 3) = "Com" Then
    
            Debug.Print "(" & i; "): " & eTR.innerText
    
        End If
    
        i = i + 1
    
    Next
    

    The immediate window then displayed

    (308): Common Stocks ... (a bunch of space) ... $5,941$5,877$5,773$3,779

    I then tested this statement:

    sd = Doc.getElementsByTagName("tr")(308).innerText
    Debug.Print sd
    

    And got the same result.