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.
pip install requests beautifulsoup4
import tkinter as tk import requests from bs4 import BeautifulSoup import webbrowser
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()
# 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])
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()
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()
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!