Make Your Own Search Engine Using Python

Make your own search engine using python. In this tutorial, we will learn how to create a search engine using python, beautiful soup , tkinter , requests .

Let’s get started.

Making GUI(Graphical User Interface) for our project

For making the GUI we will be using tkinter. Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.

So for this project we will be needing a search bar and a search button but yes in order to make our GUI attractive we will add some styling and headings as well.

For making the GUI we need to run the following commands:

pip install requests
pip install bs4

After installing, we will be writing the code to make the structure

So, here is the code for building the GUI structure:

import requests,webbrowser
from bs4 import BeautifulSoup
from tkinter import *
struct=Tk()
struct.geometry("354x350") #Defining Size of GUI box
struct.title("My Search Engine") 
label=Label(struct,text="Personal Search Engine",bg="teal",fg="white",font=("Times",20,"bold"))
label.pack(side=TOP) 
struct.config(background="teal")
text=StringVar()
def search():
    pass
label=Label(struct,text="Enter here to search",bg="teal",fg="white",font=("Times",15,"bold"))
label.place(x=50,y=100)
enter=Entry(struct,font=("Times",10,"bold"),textvar=text,width=30,bd=2,bg="white")
enter.place(x=50,y=130)
button=Button(struct,text="Search",font=("Times",10,"bold"),width=30,bd=2,command=search)
button.place(x=50,y=170)
struct.mainloop()

After writing the above code we are done with designing the GUI of our project and we will get this window as output.

Now, as the structure is ready we will be writing code for the functioning of our project.

We will be defining the function search which will open google search in backend and will fetch all url for top 5 results .

After finding the top 5 urls google chorome will automatically open all the 5 tabs right after you click the search butthon in our GUI box.

So, bellow is the entire code after defining the search function.

Source Code

import requests,webbrowser
from bs4 import BeautifulSoup
from tkinter import *
struct=Tk()
struct.geometry("354x350")
struct.title("My Search Engine")
label=Label(struct,text="Personal Search Engine",bg="teal",fg="white",font=("Times",20,"bold"))
label.pack(side=TOP)
struct.config(background="teal")
text=StringVar()
def search():
     data=requests.get('https://www.google.com/search?q='+text.get())
     soup=BeautifulSoup(data.content,"html.parser")
     result=soup.select(".kCrYT a")
     for link in result[:5]:
         searching=link.get("href")
         searching=searching[7:]
         searching=searching.split("&")
         webbrowser.open(searching[0])
label=Label(struct,text="Enter here to search",bg="teal",fg="white",font=("Times",15,"bold"))
label.place(x=50,y=100)
enter=Entry(struct,font=("Times",10,"bold"),textvar=text,width=30,bd=2,bg="white")
enter.place(x=50,y=130)
button=Button(struct,text="Search",font=("Times",10,"bold"),width=30,bd=2,command=search)
button.place(x=50,y=170)
struct.mainloop()

We searched for studygyaan in search box

As soon as we click the search button these 5 tabs were automatically opened showing the search result.