Улучши вид кода.

import requests
import tkinter as tk
from bs4 import BeautifulSoup


SEARCH_ENGINES = {
“Google”: “https://www.google.com/search?q={q}&hl={language}&tbm={result_type}”,
“Bing”: “https://www.bing.com/search?q={q}&setlang={language}&ensearch={result_type}”,
“Yahoo”: “https://search.yahoo.com/search?p={q}&ei={language}&tab={result_type}”
}


class Application(tk.Frame):
def init(self, master=None):
super().init(master)
self.master = master
self.grid()

# Create widgets for query 1
self.query_label1 = tk.Label(self, text=“Query:”)
self.query_label1.grid(row=0, column=0)
self.query_entry1 = tk.Entry(self)
self.query_entry1.grid(row=1, column=0)

self.engine_label1 = tk.Label(self, text=“Search engine:”)
self.engine_label1.grid(row=0, column=1)
self.engine_var1 = tk.StringVar(value=list(SEARCH_ENGINES.keys())[0])
self.engine_menu1 = tk.OptionMenu(self, self.engine_var1, *SEARCH_ENGINES.keys())
self.engine_menu1.grid(row=1, column=1)

self.language_label1 = tk.Label(self, text=“Language:”)
self.language_label1.grid(row=0, column=2)
self.language_var1 = tk.StringVar(value=“en”)
self.language_menu1 = tk.OptionMenu(self, self.language_var1, “en”, “fr”, “de”, “ru”)
self.language_menu1.grid(row=1, column=2)

self.result_type_label1 = tk.Label(self, text=“Result type:”)
self.result_type_label1.grid(row=0, column=3)
self.result_type_var1 = tk.StringVar(value=“”)
self.result_type_menu1 = tk.OptionMenu(self, self.result_type_var1, “”, “Images”, “Videos”, “News”)
self.result_type_menu1.grid(row=1, column=3)

self.results_label1 = tk.Label(self, text=“Number of results:”)
self.results_label1.grid(row=0, column=4)
self.results_var1 = tk.IntVar(value=1)
self.results_scale1 = tk.Scale(self, variable=self.results_var1, from_=1, to=10, orient=tk.HORIZONTAL)
self.results_scale1.grid(row=1, column=4)

self.search_button1 = tk.Button(self, text=“Search”, command=self.search_web1)
self.search_button1.grid(row=1, column=5)

self.results_text1 = tk.Text(self, height=15, width=80)
self.results_text1.grid(row=2, column=0, columnspan=6)

# Create widgets for query 2
self.query_label2 = tk.Label(self, text=“Query:”)
self.query_label2.grid(row=3, column=0)
self.query_entry2 = tk.Entry(self)
self.query_entry2.grid(row=4, column=0)

self.engine_label2 = tk.Label(self, text=“Search engine:”)
self.engine_label2.grid(row=3, column=1)
self.engine_var2 = tk.StringVar(value=list(SEARCH_ENGINES.keys())[0])
self.engine_menu2 = tk.OptionMenu(self, self.engine_var2, *SEARCH_ENGINES.keys())
self.engine_menu2.grid(row=4, column=1)

self.language_label2 = tk.Label(self, text=“Language:”)
self.language_label2.grid(row=3, column=2)
self.language_var2 = tk.StringVar(value=“en”)
self.language_menu2 = tk.OptionMenu(self, self.language_var2, “en”, “fr”, “de”, “ru”)
self.language_menu2.grid(row=4, column=2)

self.result_type_label2 = tk.Label(self, text=“Result type:”)
self.result_type_label2.grid(row=3, column=3)
self.result_type_var2 = tk.StringVar(value=“”)
self.result_type_menu2 = tk.OptionMenu(self, self.result_type_var2, “”, “Images”, “Videos”, “News”)
self.result_type_menu2.grid(row=4, column=3)

self.results_label2 = tk.Label(self, text=“Number of results:”)
self.results_label2.grid(row=3, column=4)
self.results_var2 = tk.IntVar(value=1)
self.results_scale2 = tk.Scale(self, variable=self.results_var2, from_=1, to=10, orient=tk.HORIZONTAL)
self.results_scale2.grid(row=4, column=4)

self.search_button2 = tk.Button(self, text=“Search”, command=self.search_web2)
self.search_button2.grid(row=4, column=5)

self.results_text2 = tk.Text(self, height=15, width=80)
self.results_text2.grid(row=5, column=0, columnspan=6)

def search_web1(self):
query = self.query_entry1.get()
search_engine = self.engine_var1.get()
language = self.language_var1.get()
result_type = self.result_type_var1.get()
n_results = self.results_var1.get()

results = self.search_web(query, search_engine, language, result_type)
self.results_text1.delete(1.0, tk.END)
if results:
results_list = results.split(“\n”)[:n_results]
for i, result in enumerate(results_list):
self.results_text1.insert(tk.END, “{}: {}\n”.format(i+1, result.strip()))
else:
self.results_text1.insert(tk.END, “No results found.”)

def search_web2(self):
query = self.query_entry2.get()
search_engine = self.engine_var2.get()
language = self.language_var2.get()
result_type = self.result_type_var2.get()
n_results = self.results_var2.get()

results = self.search_web(query, search_engine, language, result_type)
self.results_text2.delete(1.0, tk.END)
if results:
results_list = results.split(“\n”)[:n_results]
for i, result in enumerate(results_list):
self.results_text2.insert(tk.END, “{}: {}\n”.format(i+1, result.strip()))
else:
self.results_text2.insert(tk.END, “No results found.”)

@staticmethod
def search_web(query: str, search_engine: str, language: str = “en”, result_type: str = “”) -> str:
try:
if not query:
raise ValueError(“Query cannot be empty.”)
url_format = SEARCH_ENGINES[search_engine]
params = {“q”: query, “language”: language, “result_type”: result_type}
url = url_format.format(**params)
response = requests.get(url, timeout=5)
response.raise_for_status()
soup = BeautifulSoup(response.text, “lxml”)
results = soup.find_all(“div”, class_=“BNeawe iBp4i AP7Wnd”)
if results:
return “\n”.join((result.get_text() for result in results))
except (requests.exceptions.RequestException, ValueError) as e:
print(e)
return None


if __name__ == "__main__":
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()