VBA scraping with javaScript - Troubles to use execScript?

ⅰ亾dé卋堺 提交于 2021-01-29 10:30:56

问题


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

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

来源:https://stackoverflow.com/questions/64288496/vba-scraping-with-javascript-troubles-to-use-execscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!