EmaSMach

Simple currency converter scraping google

Jan 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. # requirements.txt
  2. """
  3. requests_html
  4. """
  5.  
  6. # currency_converter.py
  7. from requests_html import HTMLSession
  8.  
  9. def convert(amount=1, _from='ars', to='usd'):
  10.     url = f'https://www.google.com/search?q={amount}+{_from}+to+{to}'
  11.     session = HTMLSession()
  12.     response = session.get(url)
  13.     html = response.html
  14.     result_selector = "#knowledge-currency__updatable-data-column > div.b1hJbf > div.dDoNo.vk_bk.gsrt > span.DFlfde.SwHCTb"
  15.     result = html.find(result_selector)[0]
  16.     return result.text
  17.  
  18.  
  19. # currency_converterUI.py
  20. from tkinter import Frame, Tk, StringVar, DoubleVar
  21. from tkinter import ttk
  22. from currency_converter import convert
  23.  
  24.  
  25. class ConverterUI(Frame):
  26.     def __init__(self, *args, **kwargs):
  27.         super().__init__(*args, **kwargs)
  28.         self.currencies = ('ars', 'usd', 'mxn', 'eur',) # Default list of currency 'symbols'
  29.         self.master.title('Currency Converter? Lol - by David Emanuel Sandoval')
  30.  
  31.         self.make_widgets()
  32.  
  33.     def make_widgets(self):
  34.         # setting up all the widgets
  35.         self._from_lbl = ttk.Label(self, text='De:')
  36.         self.to_lbl = ttk.Label(self, text='a:')
  37.         # Variables for the comboboxes
  38.         self._from = StringVar()
  39.         self.to = StringVar()
  40.         self._from_combo = ttk.Combobox(self, values=self.currencies, textvariable=self._from)
  41.         self.to_combo = ttk.Combobox(self, values=self.currencies, textvariable=self.to)
  42.         # second row widgets
  43.         self.amount_lbl = ttk.Label(self, text='Cantidad:')
  44.         self.result = StringVar()
  45.         self.result.set('Recalculando... :-)')
  46.         self.amount = DoubleVar()
  47.         self.amount.set(1)
  48.         self.amount_spin = ttk.Spinbox(self, from_=0, to=9999999, textvariable=self.amount)
  49.         self.convert_btn = ttk.Button(self, text='Calcular', command=self.convert)
  50.         self.result_lbl = ttk.Label(self, textvariable=self.result)
  51.         # placing
  52.         self._from_lbl.grid(row=0, column=0)
  53.         self.to_lbl.grid(row=0, column=2)
  54.         self._from_combo.grid(row=0, column=1)
  55.         self.to_combo.grid(row=0, column=3)
  56.         self.amount_lbl.grid(row=1, column=0)
  57.         self.amount_spin.grid(row=1, column=1)
  58.         self.convert_btn.grid(row=1, column=2)
  59.         self.result_lbl.grid(row=1, column=3)
  60.  
  61.     def convert(self):
  62.         amount = self.amount.get()
  63.         _from = self._from.get()
  64.         to = self.to.get()
  65.         result = convert(amount, _from, to)
  66.         result = f'Resultado: {result}'
  67.         self.result.set(result)
  68.  
  69.  
  70. def main():
  71.     root = Tk()
  72.     w = ConverterUI()
  73.     w.pack()
  74.     root.mainloop()
  75.  
  76. if __name__ == '__main__':
  77.     main()
Add Comment
Please, Sign In to add comment