1 Attachment(s)
How do you get variable data from a website
I want to get variable price from https://www.tradingview.com/symbols/EURUSD/
Attachment 191716
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?
Re: How do you get variable data from a website
Quote:
Originally Posted by
yossi321
I want to get variable price from
https://www.tradingview.com/symbols/EURUSD/
Attachment 191716
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.
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.
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
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
Re: How do you get variable data from a website
Thank you
But I need rates from other currency and from other source