perform Google Search using Python

Google is the most popular search engine. It is widely used in almost every country for searching. To search for something in the Google Search engine, first, we need to open our favorite web browser. Then, we need to visit google.com and type the query we want to search. The result will appear in the browser after searching. But sometimes, we also have to access the Google search programmatically for automation or building an AI assistant.

In this tutorial, we will learn how to perform a google search using the python programming language. To follow this tutorial, the basic requirements are python installation and a basic code editor. It is also recommended to use an IDE such as the open-source VS Code for writing the codes.

Installation

To follow this tutorial, we will need to install two external python packages. The first one is the pywhatkit package, and the second one is the google package. Both of these packages can be installed using the pip package manager of python. For example, to install the pywhatkit library, fire up your terminal and run the following command in the terminal.

pip install pywhatkit

On running the above command, the stable version of the pywhatkit library will be installed in your environment. Next, we need to install the google library into our environment. Run the following command in the terminal to install the google package.

pip install google

We have installed both the required libraries. Now, let us see how to use these libraries for performing a google search.

Using PywhatKit library

The pywhatkit library is a simple library that can be used to perform a google search. Though it provides many other features like playing youtube videos, sending WhatsApp messages, etc. It is also an excellent package for performing a google search. The pywhatkit library provides a search() method that can be used for performing a google search. The following code shows a simple google search for the word codeunderscored.

# importing the search function from the pywhatkit library
from pywhatkit import search
# initializing a variable with the text that we want to search
query = "codeunderscored"
# Displaying the text that we want to search
print(f"Searching for the query : {query}")
# searching the text using teh search() function
search(query)

In the above code, first, we imported the search() method of the pywhatkit library. Next, we initialize a string variable with the text that we want to search in google. Then, at last, we pass the query text into the argument of the search() method.

The pywhatkit will open the browser and then search for the query text into the google search engine and show the result. If you already have the browser running, the search will be done by opening a new tab. Otherwise, the search will perform by opening a new window. On running the above code, the default browser will be opened and, we will see something as shown in the below image.

searching a simple text in google using the pywhatkit library
searching a simple text in google using the pywhatkit library

Searching the user input

Let us see how to prompt the user to enter a text and then search the text in the google search engine. We will use the input() function of python to take the text from the user. Next, we will use the search() function of the pywhatkit library to perform a google search for the text. See the following code for an illustration.

# importing the search function from the pywhatkit library
from pywhatkit import search
# initializing a variable with the input
# text that user wants to search
query = input("Enter the text to search : ")
print(f"Searching...")
# searching the text using teh search() function
search(query)

On running the above code, we will prompt to enter a text to search in the terminal, as shown in the below image.

searching the user input text in google using pywhatkit(terminal output)
searching the user input text in google using pywhatkit ( terminal output )

After entering a text into the above input, a new tab will open with a google search for the entered text, as shown in the below image.

searching the user input text in google using the pywhatkit library
searching the user input text in google using the pywhatkit library

Searching multiple queries

We can also search for multiple queries using the pywhatkit library. To search for multiple queries, we need to call the search() function multiple times. See the below code for a practical demonstration.

# importing the search function from the pywhatkit library
from pywhatkit import search
# initializing some variables with texts to search
text1 = "codeunderscored"
text2 = "codeunderscored python"
text3 = "django"
text4 = "codeunderscored pandas"
# Searching all the text at once
search(text1)
search(text2)
search(text3)
search(text4)

On running the above code, all the declared texts will be searched in the browser. Each of the searched results will be displayed in a new tab in the browser.

Using the google library

While installing the pywhatkit library, we also installed an additional library with the name google. The google library provides a search() function. We can use the search() function to search for text in google. This function searches for the text in google and returns a generator object for the search result URLs. The syntax for the search() method is as shown below.

search(query, tld=’co.in‘, lang=’en‘, num=10, start=0, stop=None, pause=2)

Parameters:

query: The query represents the text that we want to search.
tld: This parameter accepts the top-level domain that will be used while searching. It can be com, co.in, co.uk, etc.
lang: This parameter is used to specify the language.
num: This parameter accepts the number of results we want to fetch.
start: The number from where fetching starts.
stop: The last result to fetch.
pause: The paused time between two consecutive HTTP requests in seconds.

The search() function will return a generator object of the search results in return.

Now, let us see how to perform a google search using the search() function of the googlesearch module. The below code shows a practical demonstration of performing a google search.

# importing the search function from
# the googlesearch library
from googlesearch import search
# The text that we want to query
query = "codeunderscored"
# using the search() function to search for the text in google
results = search(query, tld="co.in", num=10, stop=10, pause=2)
# displaying the searched result links
for result in results:
    print(result)

In the above code, we first imported the search() function from the googlesearch module. Then, we created a variable to store the text we wanted to search in the second line. Next, we call the search() function and pass the query text as an argument to the search() function. At last, we displayed the searched result using the for loop and print() python function. On running the above code, we will get the output as shown in the below image.

performing a google search for a text using the search() method of the google package of python
performing a google search for a text using the search() method of the google package of python

Conclusion

In this tutorial, we have discussed how to use python to perform a google search programmatically. In addition, we have discussed two modules, viz. pywhatkit and google, for performing the google search. You may also want to see our step-by-step guide on creating and reading QR codes using python.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *