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