Create you own Search Engine like Google
In this project, we will create a search engine like google in python shell using google search package. The program will take a search input from the user and then it will display some result snippets. The result snippet include Title of the website and the url of that page. The program will display 6 results, but you can change the number of results to be shown on the first page.
The program is using 3 python packages to show the result. First one is requests, second is bs4 and the last module is google. You can install these packages using the commands :
- pip install requests
- pip install bs4
- pip install google
Once, you have installed all the three packages, let's start creating the program.
The program is basically using two sets of code. First one is to get the results and second one is to get the title of each page. To understand this program, you should have basic knowledge of:
- Installing packages
- Modules in Python
- For loop in Python
- Define function()
Code:
import requests
from bs4 import BeautifulSoup
try :
from googlesearch import search
except:
print("First install google module using command 'pip install google'")
def mainSearch():
sqr = input("Search: ")
print("Showing 6 results from the web")
results(sqr)
def results(query):
for j in search(query, tld="co.in",num=6, stop=6, pause=2):
print("-"*100)
url = str(j)
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
for title in soup.find_all('title'):
print("Title: ",title.get_text())
print("Url: ",url)
mainSearch()
The output will be like this:
Search: How to bake a cake?
Showing 6 results from the web
--------------------------------------------------------------------------------
Title: How to Make Cake at Home: Homemade Cake Recipe, Bake a Cake at Home & Cake Ingredients
Url: https://recipes.timesofindia.com/recipes/homemade-cake/rs54404412.cms
--------------------------------------------------------------------------------
Title: Classic Vanilla Cake Recipe | How to Make Birthday Cake - YouTube
Url: https://www.youtube.com/watch?v=qtlhdIfojmc
--------------------------------------------------------------------------------
Title: Chocolate Cake Recipe | How to Make Chocolate Cake - YouTube
Url: https://www.youtube.com/watch?v=2XBvw_Ty-C0&vl=en
--------------------------------------------------------------------------------
Title: How to Make a Cake from Scratch as Delicious as Your Favorite Bakery | Better Homes & Gardens
Title: BHG Logo
Title: BHG Real Estate
Url: https://www.bhg.com/recipes/how-to/bake/how-to-make-a-cake/
--------------------------------------------------------------------------------
Title: 4 Ways to Bake a Cake - wikiHow
Url: https://www.wikihow.com/Bake-a-Cake
--------------------------------------------------------------------------------
Title: 403 Forbidden
Url: https://sallysbakingaddiction.com/cake-baking-tips/
Tags:
Advanced project