Option 1: Using XE’s Official Currency Data API
XE offers a Currency Data API that provides real-time, accurate, and reliable
exchange rate data for over 220 currencies, including USD to INR. This API is
integrated with more than 100 reputable global sources, ensuring high
accuracy and up-to-date rates. It also supports historical rates, monthly
averages, and currency volatility data.
To use this API, you'll need to sign up for a free trial or a paid plan on their
website. Once you have access, you can use the provided SDKs or make
HTTP requests to fetch the exchange rates.
For Python integration, XE provides SDKs and detailed documentation to help
you get started. You can find more information and sign up for access here:
XE.com
Option 2: Using Web Scraping (Not Recommended for Production)
If you prefer not to use XE’s API, you can scrape the exchange rate directly
from their website using Python. However, this method is not recommended
for production use due to potential legal and ethical concerns, as well as the
possibility of the website's structure changing, which could break your code.
Here's a basic example using requests and
BeautifulSoup:lifeofpentester.blogspot.com+1GitHub+1
python
CopyEdit
import requests
from bs4 import BeautifulSoup
def get_usd_inr():
url = "https://www.xe.com/currencyconverter/convert/?
Amount=1&From=USD&To=INR"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
rate = soup.find('span', class_='uccResultAmount').text
return float(rate)
usd_inr = get_usd_inr()
print(f"1 USD = {usd_inr} INR")
Please note that web scraping should be done responsibly, adhering to the
website's terms of service.
Option 3: Using Alternative Python Libraries
If you prefer not to use XE's API, there are other Python libraries that provide
currency conversion services:
forex-python: A free library that supports currency conversion and
historical rates. It uses Fixer.io as its data source.PyPI+1GitHub+1
python
CopyEdit
from forex_python.converter import CurrencyRates
cr = CurrencyRates()
rate = cr.get_rate('USD', 'INR')
print(f"1 USD = {rate} INR")
pyxrate: A lightweight Python package for retrieving and using foreign
exchange rates. It leverages the Exchange API by Fawaz Ahmed.PyPI
python
CopyEdit
from currency_exchange import converter
currency_client = converter.CurrencyConverter()
rate = currency_client.get_exchange_rate('USD', 'INR')
print(f"1 USD = {rate} INR")
These libraries are easy to integrate and can be a good alternative if you
don't want to use XE's API.