-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcurrency_converter_xrates.py
35 lines (31 loc) · 1.31 KB
/
currency_converter_xrates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
from bs4 import BeautifulSoup as bs
from dateutil.parser import parse
from pprint import pprint
def get_exchange_list_xrates(currency, amount=1):
# make the request to x-rates.com to get current exchange rates for common currencies
content = requests.get(f"https://www.x-rates.com/table/?from={currency}&amount={amount}").content
# initialize beautifulsoup
soup = bs(content, "html.parser")
# get the last updated time
price_datetime = parse(soup.find_all("span", attrs={"class": "ratesTimestamp"})[1].text)
# get the exchange rates tables
exchange_tables = soup.find_all("table")
exchange_rates = {}
for exchange_table in exchange_tables:
for tr in exchange_table.find_all("tr"):
# for each row in the table
tds = tr.find_all("td")
if tds:
currency = tds[0].text
# get the exchange rate
exchange_rate = float(tds[1].text)
exchange_rates[currency] = exchange_rate
return price_datetime, exchange_rates
if __name__ == "__main__":
import sys
source_currency = sys.argv[1]
amount = float(sys.argv[2])
price_datetime, exchange_rates = get_exchange_list_xrates(source_currency, amount)
print("Last updated:", price_datetime)
pprint(exchange_rates)