-
May 21st, 2024, 09:58 AM
#1
Thread Starter
Addicted Member
How do you get variable data from a website
I want to get variable price from https://www.tradingview.com/symbols/EURUSD/
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?
-
May 21st, 2024, 10:09 AM
#2
Re: How do you get variable data from a website
 Originally Posted by yossi321
I want to get variable price from https://www.tradingview.com/symbols/EURUSD/
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.
-
May 21st, 2024, 10:59 AM
#3
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.
-
May 21st, 2024, 11:43 AM
#4
Thread Starter
Addicted Member
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
-
May 21st, 2024, 04:30 PM
#5
Addicted Member
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
-
May 21st, 2024, 07:59 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|