+ Reply to Thread
Results 1 to 10 of 10

Get Currency from Web

Hybrid View

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Get Currency from Web

    Hi All

    First time Ever doing web scraping...

    I am wanting to extract USD to ZAR exchange rate

    Any suggestions on how to get the correct element and change values...

    Sub Auto_Open()
    Dim URL As String, ExRt As Double, Obj As Object, Ele As Object
    URL = "https://www.xe.com"
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate URL
        Do Until .ReadyState = 4: DoEvents: Loop
        Stop
        Set Obj = .document.getElementsByClassName("CurrencyInputWrapper-Djuqw fmFlNB")
        For Each Ele In Obj
            Debug.Print Ele.innerText
        Next Ele
    End With
    End Sub
    Untitled.png
    Last edited by Sintek; 07-12-2020 at 09:29 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  2. #2
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Get Currency from Web

    Using the URL you've given in your code, may be this;

    Sub Test()
        'Haluk - 12/07/2020
        '
        Dim IE As Object, URL As String, i As Integer, elapsedTime As Long
        Dim RetVal As Object
        
        Const MAX_WAIT_SEC As Long = 5
        
        URL = "https://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=ZAR"
    
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate URL
        
        elapsedTime = Timer
        
        Do While Not IE.ReadyState = 4
            DoEvents
        Loop
        
        Do
            If IE.Document.getElementsByClassName("sc-fzozJi dteCCc").Length > 0 Then Exit Do
            If Timer - elapsedTime > MAX_WAIT_SEC Then
                MsgBox "Waiting for too much..... aborted!"
                GoTo SafeExit:
            End If
        Loop
        
    '   Alternative-1:
        MsgBox IE.Document.getElementsByClassName("sc-fzozJi dteCCc")(0).innerText      ' ZAR >> USD
        MsgBox IE.Document.getElementsByClassName("sc-fzozJi dteCCc")(1).innerText      ' USD >> ZAR
        
    '   Alternative-2:
        Set RetVal = IE.Document.querySelectorAll(".sc-fzozJi.dteCCc")
        
        For i = 0 To RetVal.Length - 1
            MsgBox RetVal.Item(i).innerText
        Next
        
    SafeExit:
        Set RetVal = Nothing
        IE.Quit
        Set IE = Nothing
    End Sub
    Last edited by Haluk; 07-12-2020 at 10:22 AM.

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Get Currency from Web

    You are an absolute star...Thanks Haluk...
    Sub Auto_Open()
    Dim URL As String, ExRt As Double
    URL = "https://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=ZAR"
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate URL
        Do Until .readyState = 4: DoEvents: Loop
        ExRt = .document.getElementsByClassName("converterresult-toAmount")(0).innerText
        Debug.Print ExRt
    End With
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Get Currency from Web

    You're welcome...

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Get Currency from Web

    It would be faster if you use API. You can register at this site for free https://currencylayer.com/ then to get API Key Access from Dashboard
    Then this code can be used
    Sub Test()
        Dim sAPIKey As String, sFrom As String, sTo As String, sURL As String
        sAPIKey = "Your APIKEY"
        sFrom = "USD": sTo = "ZAR"
        sURL = "http://apilayer.net/api/live?access_key=" & sAPIKey & "&currencies=" & sTo & "&source=" & sFrom & "&format=1"
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", sURL, False
            .send
            Debug.Print Val(Split(Split(.responseText, "" & sFrom & "" & sTo & """:")(1), vbLf)(0))
        End With
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Get Currency from Web

    Fantastic...Thanks Yasser

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Get Currency from Web

    You're welcome. Thanks for the rep. points.

  8. #8
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Get Currency from Web

    @sintek;

    If you don't have to use the URL that you have given in your first message, then more effective alternatives can be offered as YasserKhalil has given in his message #5

    Another alternative is; to get "Live Data" from investing.com without the need of a Web API, where XMLHTTP library is used.

    In VBE, add the reference "Microsoft HTML Object Library" from "Tools>> References" menu. Then, run the code below:

    Sub GetData()
        ' Haluk-13/07/2020
        '
        ' Reference: Microsoft HTML Object Library
        ' ---------------------------------------
        '
        Dim HTTP As Object, HTML As HTMLDocument
        Dim URL As String
       
        URL = "https://www.investing.com/currencies/single-currency-crosses"
       
        Set HTTP = CreateObject("MSXML2.XMLHTTP")
        Set HTML = New HTMLDocument
       
        HTTP.Open "GET", URL, False
        HTTP.send
       
        If HTTP.Status = 200 Then
            HTML.body.innerHTML = HTTP.responseText
            
            Set Spans = HTML.getelementsByTagName("span")
            
            For Each xSpan In Spans
                If xSpan.getAttribute("data-name") = "USD/ZAR" Then
                    spanID = xSpan.getAttribute("data-id")
                    Exit For
                End If
            Next
        
            myID = ".pid-" & spanID
            
            myMsg = "Live Currency Cross - " & xSpan.getAttribute("data-name") & " (investing.com)" & vbCrLf & vbCrLf
            
            myMsg = myMsg & "Bid: " & Chr(9) & HTML.querySelector(myID & "-bid").innerText & vbCrLf & _
                            "Ask: " & Chr(9) & HTML.querySelector(myID & "-ask").innerText & vbCrLf & _
                            "High: " & Chr(9) & HTML.querySelector(myID & "-high").innerText & vbCrLf & _
                            "Low:" & Chr(9) & HTML.querySelector(myID & "-low").innerText & vbCrLf & _
                            "Time:" & Chr(9) & HTML.querySelector(myID & "-time").innerText
                    
            MsgBox myMsg
        End If
        
        Set HTML = Nothing
        Set HTTP = Nothing
    End Sub
    Note: For different currency-crosses, all you need is to change the string in red color below accordingly (such as USD/EUR)

    If xSpan.getAttribute("data-name") = "USD/ZAR" Then
    Last edited by Haluk; 07-13-2020 at 04:50 AM.

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Get Currency from Web

    Thanks for all the options guys...

  10. #10
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Get Currency from Web

    You're welcome...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Change currency in quotation (automatically if an certain currency is selected.
    By The excel apprentice in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 04-29-2021, 03:31 AM
  2. Replies: 2
    Last Post: 09-26-2018, 03:19 PM
  3. [SOLVED] Formula that contains currency not returning the results as currency
    By Triscia in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 07-17-2014, 03:02 PM
  4. Replies: 7
    Last Post: 06-17-2014, 02:15 PM
  5. Currency conversion insert currency symbol
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-01-2013, 05:50 AM
  6. Replies: 5
    Last Post: 06-30-2011, 07:48 AM
  7. Currency Formatting-range of number as currency
    By kmurray24 in forum Excel General
    Replies: 1
    Last Post: 01-09-2008, 09:09 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1