Results 1 to 6 of 6

Thread: How do you get variable data from a website

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    160

    How do you get variable data from a website

    I want to get variable price from https://www.tradingview.com/symbols/EURUSD/
    Name:  SharedScreenshot.jpg
Views: 115
Size:  15.4 KB
    I saw in the inspect, regarding the data, the line
    HTML Code:
    span class="last-JWoJqCpY js-symbol-last">
    I tried to get the data by
    Code:
    Html.GetElementsByClassName("last-JWoJqCpY js-symbol-last").item (0)
    the output was
    [object HTMLSpanElement]
    but I need the price
    How can I do that?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,585

    Re: How do you get variable data from a website

    Quote Originally Posted by yossi321 View Post
    I want to get variable price from https://www.tradingview.com/symbols/EURUSD/
    Name:  SharedScreenshot.jpg
Views: 115
Size:  15.4 KB
    I saw in the inspect, regarding the data, the line
    HTML Code:
    span class="last-JWoJqCpY js-symbol-last">
    I tried to get the data by
    Code:
    Html.GetElementsByClassName("last-JWoJqCpY js-symbol-last").item (0)
    the output was
    [object HTMLSpanElement]
    but I need the price
    How can I do that?
    Trying to parse html is nearly always the wrong approach - one because it is against the T&Cs https://www.tradingview.com/policies/ and two because your code is always fragile and can fail if the designers make just about any change to the page layout.

    If you want to obtain this kind of information you are probably better off looking for a site that has a supported API that is designed for programmatic access.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,266

    Re: How do you get variable data from a website

    Here's a way to do it with the XMLHTTP object. I did it all late-bound, so no references at all are required.

    Just throw this in a Form1, execute the project in the IDE, and click the form.

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        '
        ' Open our XMLHTTP object.
        Dim moXmlHttp As Object
        Set moXmlHttp = CreateObject("Microsoft.XmlHttp")
        '
        ' Set our URL.
        moXmlHttp.Open "GET", "https://www.tradingview.com/symbols/EURUSD/", False
        '
        ' Make sure we don't fetch a cached copy.
        moXmlHttp.setRequestHeader "Cache-Control", "no-cache, no-store, must-revalidate"
        moXmlHttp.setRequestHeader "Pragma", "no-cache"
        moXmlHttp.setRequestHeader "Expires", "0"
        '
        ' Send our "GET" request.
        moXmlHttp.Send
        '
        ' Get the page's HTML.
        Dim sHtml As String
        sHtml = moXmlHttp.responseText
        '
        ' Find what we're looking for.
        Dim i As Long
        i = InStr(sHtml, "The current rate of EURUSD is ")
        If i = 0& Then
            Debug.Print "Couldn't find the rate."
            Exit Sub
        End If
        '
        ' Parse and report.
        sHtml = Mid$(sHtml, i + Len("The current rate of EURUSD is "))
        i = InStr(sHtml, " ")
        Dim sRate As String
        sRate = Left$(sHtml, i - 1&)
        Debug.Print "The current rate of EURUSD is "; sRate
        '
        ' Close our XMLHTTP object.
        Set moXmlHttp = Nothing
    End Sub
    
    
    Not sure I got the exact same rate as what's appearing in a browser, but it's definitely a rate in the HTML code. That dynamic rate is probably coming out of some Java Script code.



    And PlausiblyDamp's warnings are still applicable. Webpage scraping is almost always a bad idea.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    160

    Re: How do you get variable data from a website

    Thank you Elroy, but the rate does not change
    and I don't want to load the whole page every time I need the rate

  5. #5
    Addicted Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    139

    Re: How do you get variable data from a website

    On my Windows 10 system,
    I just shell out and run a console program;
    Code:
    U:\>"C:\Program Files\Qalculate\qalc.exe" 1.00 EUR to USD 
    1 EUR = approx. USD 1.086400000
    Capture the output,
    parse the output,
    and you have the EUR/USD value.

    The program is available from https://qalculate.github.io/

    Joe

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    160

    Re: How do you get variable data from a website

    Thank you
    But I need rates from other currency and from other source

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width