How to perform google search in Python
There are many times when you want to test the capabilities of a programming language like Python. There is a lot of cool stuff that you can do with Python and one of them is Web Scrapping. In this tutorial, we are going to learn how you can perform google search using Python and in a way create your personal search engine very easily. Before proceeding further let us see a basic idea of Web Scrapping.
Basically, Web Scrapping is a tool used by programmers to extract information from websites. And today, we are going to do the basics of it with an example of how to perform google search in Python.
Google Search Using Python
We are going to use Python library which is called as google, which will help us to web-scrape the search results using our python code and show it to us in the respective editor’s terminal.
Follow the steps given below:
- Install python library called Beautiful Soup or often referred as BS4.
Beautiful Soup is a library that makes it easy to scrape information from web pages.
pip install BeautifulSoup
- Install another python library called as Google.
This library will help you to access google search results via python.
pip install google
- After doing this its time to import installed libraries.
from googlesearch import search
We are going to use SEARCH function provided within the python’s google library.
Syntax of search is as follows
search(searchfor, tld=’.edu’, lang=’en’, num=10, start=0, stop=None, pause=2.0)
So here you will see there are several attributes present in search function. No need to panic, they are very easy to understand. Let’s take a look at this attribute one by one.
searchfor is a variable that stores the value that you need to search.
lang is nothing but the language of your search and it is optional.
num is variable that justifies the number of links in your result.
start and stop specifies the starting and ending index of search result. So if start =1 that means the link on the zeroth position will be skipped and stop = 1 that means you will get only one result. You can adjust the value of start and stop accordingly.
tld is the extension of your search that means it holds the domain you need to search like .com, .in, .edu, .mil, .co.in, etc.
pause is the time frame for the result to scrape. Remember the value of pause should not be too long or too short. In our opinion pause at 2 or 3 works fine.
That’s the search function that will do all the tasks for us. Now we just need to enter the string we are searching for, loop through our search function and finally printing the result.
- Easiest possible code for google search using python is given below:
from googlesearch import search searchfor = input("Search For:") print ("Processing Your Requests...") for result in search(searchfor, tld="com", num=15, stop=15, pause=2): print(result)
- We first imported libraries.
- Took input from user for the string to search.
- Passed the string as one of the attributes in the search function.
- Then we set the values for remaining attributes in search function. Remember if stop = 0, it will keep running in loop forever and will give you ‘n’ no.of outputs. So never set value of stop = 0.
- Finally, we printed the result.
- Look for output below.
Output will be shown into your editor’s terminal as: