-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchEngine.py
More file actions
65 lines (55 loc) · 2.42 KB
/
searchEngine.py
File metadata and controls
65 lines (55 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# main search engine
import tkinter as tk
from PIL import ImageTk, Image
from retriever import initialize, search
import webbrowser
from urllib.request import urlopen
from bs4 import BeautifulSoup
class Engine:
def __init__(self, width, height):
self.width = width
self.master = tk.Tk()
self.master.title("Search Engine")
self.master.geometry(f"{width}x{height}")
logo = ImageTk.PhotoImage(Image.open("logo.png"))
panel = tk.Label(self.master, image=logo)
panel.place(relx = 0.5, rely = 0.2, anchor=tk.CENTER)
query = tk.Entry(self.master)
query.place(relx=0.5, rely=0.3, anchor=tk.CENTER)
search_button = tk.Button(self.master, text = "Search", command=lambda q=query: self.retrieve(query.get()))
search_button.place(relx=0.5, rely=0.35, anchor=tk.CENTER)
self.result = tk.Label(self.master)
self.q = tk.Label(self.master)
self.master.mainloop()
def retrieve(self, query):
results = search(query)
# display results
self.displayResults(query, results)
def getPageTitle(self, url):
soup = BeautifulSoup(urlopen(url), 'lxml')
return soup.title.string
def displayResults(self, query, results):
if len(results) == 0:
q = tk.Label(self.master, text=f"No results found for {query}", width = self.width)
q.place(relx=0.5, rely = 0.4, anchor = tk.N)
y = 0.45
for i in range(5):
self.result = tk.Label(self.master, text=f" \n ", width = self.width)
self.result.place(relx=0.5, rely = y, anchor = tk.N)
y += .1
else:
self.q = tk.Label(self.master, text=f"Found {len(results)} results for {query}, here are the top 5 results:", width=self.width)
self.q.place(relx=0.5, rely = 0.4, anchor = tk.N)
y = 0.45
for x in range(0,5):
try:
pageTitle = self.getPageTitle(results[x])
self.result = tk.Label(self.master, text=f"{pageTitle}\n{results[x]}", width = self.width)
except:
self.result = tk.Label(self.master, text=f" \n ", width = self.width)
finally:
self.result.place(relx=0.5, rely = y, anchor = tk.N)
y += .1
if __name__ == "__main__":
initialize()
e = Engine(800,600)