Build Your Search Engine Project with Python: Document & Web Search

Search engines are an essential tool for accessing information quickly. How about creating your own search engine that can search through both predefined documents and the web? In this comprehensive guide, we’ll walk you through the process of building a combined search engine using Python’s Tkinter library for the graphical user interface (GUI) and incorporating document-based searching and web searching functionalities.

Lets create a search engine like google, microsoft bing in python.

image 6

Prerequisites

Before we begin, ensure you have:

  • Python Installed: If not installed, download and install Python from the official Python website.
  • Tkinter Library: Tkinter comes pre-installed with most Python distributions.
  • Requests and BeautifulSoup Libraries: You’ll need these libraries for web scraping. Install them using:
pip install requests beautifulsoup4

Building the Search Engine Like Google, Bing using Python, Tkinter and BeautifulSoup

We’ll create a search engine with a GUI that allows you to search through both predefined documents and the web. Here’s a step-by-step guide:

  1. Import Required Libraries:
import tkinter as tk
import requests
from bs4 import BeautifulSoup
import webbrowser
  1. Create the GUI Window:
root = tk.Tk()
root.title("Combined Search Engine")
  1. Create Query Entry and Results Text Widgets:
query_entry = tk.Entry(root)
query_entry.pack()

results_text = tk.Text(root, height=10, width=40)
results_text.pack()
  1. Implement Document-Based Search:
# Sample documents
documents = [
    "Python is a programming language.",
    "Search engines help us find information.",
    "Machine learning is a subset of AI.",
    "Data analysis involves processing data.",
    "StudyGyaan is a Great Website for Python, Django, Flask, Spring Boot"
    # Add more documents
]

def search_documents():
    query = query_entry.get().lower()
    matching_documents = []

    for i, document in enumerate(documents):
        if query in document.lower():
            matching_documents.append(documents[i])

    results_text.delete(1.0, tk.END)  # Clear previous results
    if matching_documents:
        results_text.insert(tk.END, "Matching Documents:\n")
        for doc in matching_documents:
            results_text.insert(tk.END, "- " + doc + "\n")
    else:
        results_text.insert(tk.END, "No matching documents found.")
  1. Implement Web Search:
def search_web():
    data = requests.get('https://www.google.com/search?q=' + query_entry.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])
  1. Create Search Buttons:
doc_search_button = tk.Button(root, text="Search Documents", command=search_documents)
doc_search_button.pack()

web_search_button = tk.Button(root, text="Web Search", command=search_web)
web_search_button.pack()
  1. Run the Tkinter Main Loop:
root.mainloop()

Full Source Code

Here’s the complete code for building a combined search engine with document-based search and web search functionalities using Python and Tkinter:

import tkinter as tk
import requests
from bs4 import BeautifulSoup
import webbrowser

# Sample documents
documents = [
    "Python is a programming language.",
    "Search engines help us find information.",
    "Machine learning is a subset of AI.",
    "Data analysis involves processing data.",
    "StudyGyaan is a Great Website for Python, Django, Flask, Spring Boot"
    # Add more documents
]

def search_documents():
    query = query_entry.get().lower()
    matching_documents = []

    for i, document in enumerate(documents):
        if query in document.lower():
            matching_documents.append(documents[i])

    results_text.delete(1.0, tk.END)  # Clear previous results
    if matching_documents:
        results_text.insert(tk.END, "Matching Documents:\n")
        for doc in matching_documents:
            results_text.insert(tk.END, "- " + doc + "\n")
    else:
        results_text.insert(tk.END, "No matching documents found.")

def search_web():
    data = requests.get('https://www.google.com/search?q=' + query_entry.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])

root = tk.Tk()
root.title("Combined Search Engine")

query_entry = tk.Entry(root)
query_entry.pack()

results_text = tk.Text(root, height=10, width=40)
results_text.pack()

doc_search_button = tk.Button(root, text="Search Documents", command=search_documents)
doc_search_button.pack()

web_search_button = tk.Button(root, text="Web Search", command=search_web)
web_search_button.pack()

root.mainloop()

Conclusion

Building a combined search engine with Python and Tkinter provides a practical way to implement both document-based and web-based searches within a single application. You can expand this search engine by refining the user interface, adding search result highlighting, and incorporating more advanced search algorithms.

As you continue your coding journey, consider implementing features such as ranking search results, handling more complex queries, and customizing the web search enzine’s behavior. By combining document search and web search functionalities, you’ll be creating a versatile tool that can help you access information efficiently. Happy coding and exploring the world of search engines!

Blogs You Might Like: