Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8a24a6

Browse files
Merge pull request avinashkranjan#103 from xayke/exchange-rates
Added currency exchange rates script
2 parents 00e0672 + 8c728db commit c8a24a6

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎CurrencyExchangeRates/exchange_rates.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from bs4 import BeautifulSoup
2+
import requests as req
3+
4+
currencies = []
5+
6+
page = req.get('https://www.x-rates.com/').text
7+
8+
soup = BeautifulSoup(page, 'html.parser')
9+
10+
options = soup.find_all('option')[:-11]
11+
12+
for option in options:
13+
currency_short = option.text[: (option.text.find(" "))]
14+
currency_name = option.text[(option.text.find(" ") + 3):]
15+
current_element = {
16+
'name': currency_name,
17+
'short': currency_short
18+
}
19+
currencies.append(current_element)
20+
print('{}. {} ({})'.format(len(currencies),
21+
current_element['name'], current_element['short']))
22+
23+
24+
currency_index = int(input('Enter your currency\'s position number: ')) - 1
25+
currency = currencies[currency_index]
26+
amount = input(
27+
'033円cEnter amount of {}s (if amount isn\'t integer, then write it with a dot, not comma): '.format(currency['name'].lower()))
28+
29+
30+
currencies_table_url = 'https://www.x-rates.com/table/?from={}&amount={}'.format(
31+
currency['short'], amount)
32+
33+
currencies_table_page = req.get(currencies_table_url).text
34+
35+
soup = BeautifulSoup(currencies_table_page, 'html.parser')
36+
37+
table_rows = soup.findChild(
38+
'table', attrs={'class': 'tablesorter'}).findChildren('tr')[1:]
39+
40+
41+
print('033円cFor {} {}s you\'ll get:'.format(amount, currency['name'].lower()))
42+
43+
for table_row in table_rows:
44+
row_data = table_row.findChildren('td')
45+
exchange_rate = {
46+
'currency': row_data[0].text,
47+
'amount': float(row_data[1].text)
48+
}
49+
print('{:.3f} {}s'.format(
50+
exchange_rate['amount'], exchange_rate['currency']))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /