How to access a value that was put via JavaScript from a site via VBA?

5

I'm trying to access the values that appear on the site: link

I was unable to access the values either by direct hyperlink in excel, or by creating a querytable via VBA. Of the two modes the values do not return. Searching the internet, I saw that the values that are entered via JavaScript can not be removed in this way, because the values do not have an ID. Well, looking at the source code I can see how the values are input by a script, but I have no idea how to bring those values from the site to Excel via VBA.

Could anyone help me?

The source code snippet where the values are entered is in bold type. When you go to the site the values will be different from the below values are stock quotes.

<SCRIPT LANGUAGE='JScript'>
 tab_A = tab_A + 'TR'
            "tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,650</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,640</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,650</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,643</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,641</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,641</TD>';
            tab_C = tab_C + '<TD ALIGN="right" CLASS="tabelaConteudo1">13,649</TD>';"

...
            </SCRIPT>
    
asked by anonymous 10.06.2015 / 21:09

2 answers

1

The problem in this case is that the values are placed on the page by a script executed after the page loads through the browser engine.

So you're going to have to use a browser. Luckily, there is one you can use and manipulate through VBA. It is the ActiveX WebBrowser control, which is nothing more than an encapsulated version of Internet Explorer, operating as if it were IE7.

You can do what was suggested here , or create a UserForm and put the WebBrowser in it.

The important thing is to navigate to the page that interests you. You can do this with this command:

WebBrowser1.Navigate("http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/BoletimOnline1.asp?caminho=&pagetype=pop&Acao=BUSCA&cboMercadoria=DI1")

You will have to have created a Sub to handle the WebBrowser.DocumentCompleted event, which will fire when the page finishes loading, and access the WebBrowser.Document property that returns you an object that gives you access to the DOM (< in> Document Object Model) of that page, allows you to read (and even change) the HTML elements and their states.

To do this, you'll have to parse the HTML code of the page to know what elements you should read, what values you should change, and what DOM events you need to trigger to get what you need.

    
28.07.2017 / 00:18
0

I could not open the page here. Anyway, from what I saw in your example, the variable is public, right?

If it is, using the InternetExplorer.Document.parentWindow.execScript method, you can create an HTML element, insert the variable text in the HTML, and then access that element with VBA.

If it does this way, I think it would look something like this:

  Dim IE as New InternetExplorer
  Dim Doc as HTMLDocument

  IE.Visible = True
  IE.Navigate2 "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/BoletimOnline1.asp?caminho=&pagetype=pop&Acao=BUSCA&cboMercadoria=DI1"

  Do While IE.readystate  READYSTATE_COMPLETE: DoEvents: Loop

  Set Doc = IE.Document
  Doc.parentWindow.execScript "var p = document.createElement('p'); p.id = 'vba'; p.innerHTML = tab_C"

  MsgBox Doc.getElementById('vba').innerHTML

Now if you can not / want to use a browser, you can can simply make an http request for the page and flip it with the HTML of it (which will probably contain the text you want).

That's the way I would, and I'd use regular expression to flip through HTML.

If you are interested:

    Dim WHTTP As New WinHttpRequest
    Dim HTML As String
    Dim RegEx As New RegExp
    Dim Matches As MatchCollection
    Dim Match As Match
    Dim Valores As New Collection
    Dim i As Double

    With WHTTP
        .Open "GET", "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/BoletimOnline1.asp?caminho=&pagetype=pop&Acao=BUSCA&cboMercadoria=DI1", False
        .send

        If .Status  200 Then
            Debug.Print "Requisição retornou status " & .Status & " (" & .statusText & ")"
            Stop
        End If

        HTML = .responseText
    End With
    Set WHTTP = Nothing

    With RegEx
        .Global = True
        .IgnoreCase = False
        .MultiLine = True
        .Pattern = "\d+(,?)\d{3}(?=)"

        Set Matches = .Execute(HTML)
    End With

    If Matches.Count > 0 Then
        For i = 0 To Matches.Count - 1
            Set Match = Matches.Item(i)

            Valores.Add Match.Value
        Next i
    End If

At the end of this code, the Valores collection will have all results that match the regular expression \d+(,?)\d{3}(?=</TD>)

    
02.07.2018 / 20:05