|
| 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