0

I’d need to create a table with the result of the earnings. I was able to manage the first page, but I need to complete the table with all the previous earnings as well. I don’t know how to “click” the “Mostrar Mas” button using exeScript, get the result and keep doing it till the table is completed

This is the code I’ve put together so far.

Sub fundamentals()
    'Primer pagina de earnings
    Dim XMLReq As New MSXML2.XMLHTTP60
    Dim HTMLDoc As New MSHTML.HTMLDocument
    Dim TRs As MSHTML.IHTMLElementCollection
    
    Dim Table As MSHTML.IHTMLTable
    Dim Rows As MSHTML.IHTMLElementCollection
    Dim TD As MSHTML.IHTMLElement

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Apple")
    
    XMLReq.Open "POST", "https://es.investing.com/equities/apple-computer-inc-earnings", False
    'XMLReq.Open "POST", "https://es.investing.com/equities/morehistory", False
    XMLReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    XMLReq.setRequestHeader "Accept", "text/plain"
    XMLReq.setRequestHeader "X-Requested-With", "XMLHttpRequest"
    
    XMLReq.send
    
    If XMLReq.Status <> 200 Then
        MsgBox "problem" & vbNewLine & XMLReq.Status & "- " & XMLReq.statusText
        Exit Sub
    End If
    
    
    HTMLDoc.body.innerHTML = XMLReq.responseText
    
    Set Table = HTMLDoc.getElementById("earningsHistory6408")
    Set Rows = Table.Rows
    Set TDs = Rows.tags("TR")
    
    i = 0: j = 8: k = 3
    For Each TD In TDs
        If i = 0 Then
            Headerarray = Split(TD.innerHTML, "<TH")
            ws.Cells(2, j) = Split(Split(Headerarray(1), ">")(1), "<")(0): j = j + 1                'Fecha de presentacion
            ws.Cells(2, j) = Split(Split(Headerarray(2), ">")(1), "<")(0): j = j + 1                'Período terminado
            ws.Cells(2, j) = Split(Split(Headerarray(3), ">")(1), "<")(0): j = j + 1                'BPA
            ws.Cells(2, j) = Split(Split(Split(Headerarray(4), ">")(1), ";")(2), "<")(0): j = j + 1 'Previsión
            ws.Cells(2, j) = Split(Split(Headerarray(5), ">")(1), "<")(0): j = j + 1                'Ingresos
            ws.Cells(2, j) = Split(Split(Split(Headerarray(6), ">")(1), ";")(2), "<")(0)            'Previsión
        Else
            TDarray = Split(TD.innerHTML, "<TD")
            ws.Cells(k, j) = Split(Split(TDarray(1), ">")(1), "<")(0): j = j + 1 'Fecha de publicacion
            ws.Cells(k, j) = Split(Split(TDarray(2), ">")(1), "<")(0): j = j + 1 'Periodo terminado
            ws.Cells(k, j) = Split(Split(TDarray(3), ">")(1), "<")(0): j = j + 1 'BPA
            ws.Cells(k, j) = Split(Split(TDarray(4), ";")(2), "<")(0): j = j + 1 'Prevision
            ws.Cells(k, j) = Split(Split(TDarray(5), ">")(1), "<")(0): j = j + 1 'Ingresos
            ws.Cells(k, j) = Split(Split(TDarray(6), ";")(2), "<")(0): j = j + 1 'Prevision
            
        End If
        i = i + 1: j = 8: k = k + 1
    Next
    
End Sub

enter image description here

I’ve tried to click the button with this code, but none of the lines are correct:

Call XMLReq. responseText.execScript("showMoreEarningsHistory(6408, this)") < Invalid qualifier

Call XMLReq.execScript("showMoreEarningsHistory(6408, this)") <- Object does not support this property or method

Inspect of Mostrar Mas button

If I manually click the button this is what I see in the dev tools:

dev tools - Network tab

Then if I run this code I get what I need, but he BIG question is how I can get the information in advance?

code

  • Please consider whether some of the answers to your prior questions are worth accepting. You also aren't sending anything in your POST. This question almost certainly has an existing answer on SO that you might be able to use to guide you. – QHarr Oct 10 '20 at 00:48
  • Hi QHarr, I'm not an expert so, apologies if you see my question answered on my previous questions. My point is that none of the answers I've received to run execScript have worked. Also in this case I'm not using IE (as in previous questions. It's too slow) but XMLHTTP60. Also, I'm asking how to capture the result of the script, I really have no idea how to catch it to parse it afterward. Lastly, in regards to not sending anything in my POST, I have to apologies as well I'm not sure what you mean or what I'm not doing. Thanks and sorry for the long answer :) – Mike Hillflag Oct 10 '20 at 04:51
  • No worries. If none of the prior answers meet your requirements then you are absolutely right not to accept them. – QHarr Oct 10 '20 at 17:14
  • With respect to the POST. If you open the network tab of dev tools (F12) and then click the Mostrar Mas, do you see an xhr (web traffic) generated in the network tab? It is likely the page is making a request for additional info to update the page. The details of that request will be shown in the network tab against that request [example](https://stackoverflow.com/questions/56277464/how-to-import-a-table-from-web-page-with-div-class-to-excel/56279841#56279841) – QHarr Oct 10 '20 at 17:16
  • Hi QHarr, I've updated the the original question. The information I see in the network tab is exactly what I'm looking for, but I'don't know when and how it was requested. It might have been a javascript code that I was not able to make it work (as I showed in above). Hope it clarifies where my doubt is. – Mike Hillflag Oct 13 '20 at 02:33
  • the network tab will show the details of the request – QHarr Oct 13 '20 at 04:00

0 Answers0