Rohan
Rohan

Reputation: 319

How to get data from table on webpage

I am trying to get data from the table under "full transaction log" but I am not sure how to get it. I tried to get the innertext of the table using ID but all I get are the headers. Can anyone guide me here please.

This is the table I want to get data from: https://screenshots.firefox.com/VDJEsuPeQLIgRYWa/www.aastocks.com

Sub test()

    Dim oDom As Object: Set oDom = CreateObject("htmlFile")
    Dim x As Long, y As Long
    Dim oRow As Object, oCell As Object
    Dim data

    With CreateObject("msxml2.xmlhttp")
        .Open "GET", "http://www.aastocks.com/en/stocks/analysis/transaction.aspx?symbol=60001", False
        .Send
        oDom.body.innerHtml = .responseText
    End With

    Set t = oDom.getElementById("tradeLog1")

    Sheet1.Range("A1") = t.innertext
'
'    'loop through each row
'    For Each r In t.Rows
'        Debug.Print r.Cells(1).innertext
'        'loop through each column in the row
'        For Each c In r.Cells
'            Debug.Print c.innertext
'        Next
'    Next

End Sub

Upvotes: 0

Views: 1952

Answers (2)

SIM
SIM

Reputation: 22440

Give this a go. It will fetch you the desired data.

Sub TableData()
    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim htmla As Object, tRow As Object, tCel As Object

    Application.ScreenUpdating = False

    With IE
        .Visible = True
        .navigate "http://www.aastocks.com/en/stocks/analysis/transaction.aspx?symbol=60001"
        Do While .readyState <> READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    html.getElementsByClassName("float_r pad5R pad5L")(0).getElementsByTagName("input")(0).Click
    Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop

    x = 1

    Set htmla = html.getElementById("tradeLog1")
    For Each tRow In htmla.Rows
        For Each tCel In tRow.Cells
            c = c + 1: Cells(x + 1, c) = tCel.innerText
        Next tCel
        c = 0
        x = x + 1
    Next tRow

    Application.ScreenUpdating = True
    IE.Quit

End Sub

Reference to add:

1. Microsoft internet controls
2. Microsoft html object library.

Upvotes: 2

K.Dᴀᴠɪs
K.Dᴀᴠɪs

Reputation: 10139

I found the elements associated with the table. I will leave it up to you on how you handle each element in your worksheet.

This has been tested and works.

Option Explicit

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub test()

    '#############################################################
    '########### You will need to make references to:  ###########
    '###########   (1) Microsoft Internet Controls     ###########
    '###########   (2) Microsoft HTML Object Library   ###########
    '#############################################################

    Dim IE As New InternetExplorer, Url$
    Url = "http://www.aastocks.com/en/stocks/analysis/transaction.aspx?symbol=60001"

    Dim iElem As Object, iElems As Object
    Dim iElem2 As Object, iElems2 As Object, Btn As Object

    With IE
        .Visible = True
        .navigate Url
        Do While .Busy Or .readyState < READYSTATE_LOADED
            DoEvents
        Loop
        Set iElems = IE.Document.getElementById("mainForm")
        Set iElems = iElems.getElementsByClassName("float_r pad5R pad5L")
        For Each iElem In iElems
            Set iElems2 = iElem.getElementsByTagName("input")
            For Each iElem2 In iElems2
                If iElem2.Value = 2 Then
                    Set Btn = iElem2
                    Exit For
                End If
            Next iElem2
        Next iElem
        Btn.Click
        Do While .Busy Or .readyState < READYSTATE_COMPLETE
            DoEvents
        Loop
        Sleep 500
    End With

    Dim ieDoc As HTMLDocument, myTbl As HTMLTable
    Dim tRow As HTMLTableRow, tCell As HTMLTableCell
    Set ieDoc = IE.Document
    Set myTbl = ieDoc.getElementById("tradeLog1")

    ' I will leave it up to you to place these in your own manner, this just shows that you are able to grab the table now
    For Each tRow In myTbl.Rows
        For Each tCell In tRow.Cells
            If tCell.innerText <> "" Then Debug.Print tCell.innerText
        Next tCell
    Next tRow

End Sub

Upvotes: 2

Related Questions