Convert Currency with Python: Where Accuracy Meets Efficiency.

Currency converter use case.


In case you need to know exchange rates information for any given currency and for automation purpose you don't want to use a browser you can use a solution below that will provide necessary information in JSON format. The Python script below will make a HTTP request to "www.x-rates.com" - a free source of financial information which includes foreign exchange rates and tools including a currency conversion calculator, historical rates and graphs, and a monthly exchange rate average.

Python currency converter.
Python currency converter meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-12-20 by Andrey BRATUS, Senior Data Analyst.




    Obtained result JSON file can be converted in any format suitable for further visualisation and financial data analysis and valuable business decisions. The script itself can be easily modified to obtain necessary historical data with any desired time aggregations.


  1. Python currency converter code:


  2. 
    import requests
    from bs4 import BeautifulSoup as bs
    from dateutil.parser import parse
    from pprint import pprint
    
    
    def currency_exchange_rates(currency, amount=1):
       
        content = requests.get(f"https://www.x-rates.com/table/?from={currency}&amount={amount}").content
       
        soup = bs(content, "html.parser")
       
        price_datetime = parse(soup.find_all("span", attrs={"class": "ratesTimestamp"})[1].text)
       
        exchange_tables = soup.find_all("table")
        exchange_rates = {}
        for exchange_table in exchange_tables:
            for tr in exchange_table.find_all("tr"):
               
                tds = tr.find_all("td")
                if tds:
                    currency = tds[0].text
                   
                    exchange_rate = float(tds[1].text)
                    exchange_rates[currency] = exchange_rate        
        return price_datetime, exchange_rates
    
    
    money='USD'
    print(f'Current exchange rates for {money} .\n')
    currency_exchange_rates(money)
    


  3. Currency converter data output:


  4. 
    Current exchange rates for USD .
    
    (datetime.datetime(2022, 9, 19, 6, 19, tzinfo=tzlocal()),
     {'Euro': 1.002244,
      'British Pound': 0.877824,
      'Indian Rupee': 79.633418,
      'Australian Dollar': 1.494969,
      'Canadian Dollar': 1.33024,
      'Singapore Dollar': 1.40886,
      'Swiss Franc': 0.968212,
      'Malaysian Ringgit': 4.545004,
      'Japanese Yen': 143.34504,
      'Chinese Yuan Renminbi': 7.011241,
      'Argentine Peso': 142.374242,
      'Bahraini Dinar': 0.376,
      'Botswana Pula': 13.107451,
      'Brazilian Real': 5.252446,
      'Bruneian Dollar': 1.40886,
      'Bulgarian Lev': 1.960218,
      'Chilean Peso': 922.255503,
      'Colombian Peso': 4433.360013,
      'Croatian Kuna': 7.539316,
      'Czech Koruna': 24.552435,
      'Danish Krone': 7.453256,
      'Hong Kong Dollar': 7.849137,
      'Hungarian Forint': 405.249921,
      'Icelandic Krona': 140.015247,
      'Indonesian Rupiah': 14974.741946,
      'Iranian Rial': 42274.697577,
      'Israeli Shekel': 3.439065,
      'Kazakhstani Tenge': 477.266915,
      'South Korean Won': 1392.419029,
      'Kuwaiti Dinar': 0.309207,
      'Libyan Dinar': 4.902175,
      'Mauritian Rupee': 45.384269,
      'Mexican Peso': 20.093581,
      'Nepalese Rupee': 127.473193,
      'New Zealand Dollar': 1.678911,
      'Norwegian Krone': 10.262878,
      'Omani Rial': 0.38502,
      'Pakistani Rupee': 237.899823,
      'Philippine Peso': 57.339692,
      'Polish Zloty': 4.722987,
      'Qatari Riyal': 3.64,
      'Romanian New Leu': 4.933766,
      'Russian Ruble': 60.227018,
      'Saudi Arabian Riyal': 3.75,
      'South African Rand': 17.698802,
      'Sri Lankan Rupee': 364.917292,
      'Swedish Krona': 10.809939,
      'Taiwan New Dollar': 31.420065,
      'Thai Baht': 36.944793,
      'Trinidadian Dollar': 6.787903,
      'Turkish Lira': 18.281413,
      'Emirati Dirham': 3.6725,
      'Venezuelan Bolivar': 802975.743786})
    




See also related topics: