Amazing-Python-Scripts

Форк
0
46 строк · 1.5 Кб
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 = {'name': currency_name, 'short': currency_short}
16
    currencies.append(current_element)
17
    print('{}. {} ({})'.format(len(currencies), current_element['name'],
18
                               current_element['short']))
19

20
currency_index = int(input('Enter your currency\'s position number: ')) - 1
21
currency = currencies[currency_index]
22
amount = 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

26
currencies_table_url = 'https://www.x-rates.com/table/?from={}&amount={}'.format(
27
    currency['short'], amount)
28

29
currencies_table_page = req.get(currencies_table_url).text
30

31
soup = BeautifulSoup(currencies_table_page, 'html.parser')
32

33
table_rows = soup.findChild('table', attrs={
34
    'class': 'tablesorter'
35
}).findChildren('tr')[1:]
36

37
print('\033cFor {} {}s you\'ll get:'.format(amount, currency['name'].lower()))
38

39
for table_row in table_rows:
40
    row_data = table_row.findChildren('td')
41
    exchange_rate = {
42
        'currency': row_data[0].text,
43
        'amount': float(row_data[1].text)
44
    }
45
    print('{:.3f} {}s'.format(exchange_rate['amount'],
46
                              exchange_rate['currency']))
47

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.