Advertisement
jimMoonrock

Untitled

Apr 21st, 2021
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4.  
  5.  
  6. def get_html(url):
  7.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0'}
  8.     response = requests.get(url, headers=headers)
  9.     html_text = response.text
  10.     return html_text
  11.  
  12. def get_total_page(html, pam):
  13.     soup = BeautifulSoup(html, 'lxml')
  14.     sorted_mainpage_with_tag = soup.find_all('a', class_="dotted")
  15.     sorted_page_with_price = soup.find_all('div', class_='course')
  16.  
  17.     return sorted_mainpage_with_tag if pam == 'main page' else sorted_page_with_price
  18.  
  19. def main():
  20.  
  21.  
  22.     convert_money_for_user = []
  23.  
  24.     for n in range(4):
  25.  
  26.         url = 'https://kurs.com.ua/#main_table'
  27.         user_inp = input("Введите еюиницу или stop, чтобы выйти ") # Курс доллара
  28.  
  29.  
  30.         if user_inp == 'stop':
  31.                 break
  32.  
  33.         how_much_need_to_convert = float(input("Введите количество денежных едниц "))
  34.         main_page = get_total_page(get_html(url), 'main page')
  35.  
  36.         for text in main_page:
  37.             if text.attrs['title'] == user_inp:
  38.                 choice_currency_user = text.attrs['href']
  39.                 page_with_price = choice_currency_user
  40.  
  41.  
  42.  
  43.                 convert_string_to_number = float(get_total_page(get_html(page_with_price),
  44.                                                                 'page with price for user')[0].text[0:5])
  45.  
  46.                 ending_currency_name = ''
  47.                 if len(user_inp.split(' ')) == 3:
  48.                     ending_currency_name = user_inp.split(' ')[1:][1][:-1]
  49.                 else:
  50.                     ending_currency_name = user_inp.split(' ')[1][:-1]
  51.  
  52.                 convert_money_for_user.append(f"ваш {ending_currency_name} {how_much_need_to_convert} = "
  53.                          f"{convert_string_to_number * how_much_need_to_convert} гривны")
  54.  
  55.  
  56.  
  57.     for currency in convert_money_for_user:
  58.         print(currency)
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     main()
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement