Google Search in Python: A Beginner’s Guide

Sagar Yadav
2 min readJul 29, 2023

Python is a versatile and powerful programming language, and with its extensive libraries and modules, it can be harnessed for a wide range of applications. One such interesting use case is performing Google searches programmatically. In this blog, we will explore how to conduct Google searches using Python and fetch the top search results.

Step 1: Installing the Required Module To get started, we need to install the googlesearch-python module, which allows us to interact with Google's search engine. Open your terminal or command prompt and run the following command:

pip install googlesearch-python

Step 2: Writing the Python Script Once the module is installed, we can write a Python script to perform Google searches and fetch the top results. Let’s create a function to handle this task:

from googlesearch import search

def fetch_top_search_results(query, num_results=10):
search_results = search(query, num_results=num_results)
return search_results

The function fetch_top_search_results takes two parameters: the query for the search term and num_results for the number of top results to fetch (default is 10).

Step 3: Prompting User for Search Query To make the script interactive, we will prompt the user to enter the search query. Let’s add the following code to the script:

if __name__ == "__main__":
search_query = input("Enter your search query: ")
top_results = fetch_top_search_results(search_query, num_results=10)

print("Top 10 search results:")
for idx, result in enumerate(top_results, 1):
print(f"{idx}. {result}")

This code snippet will ask the user to input the search query. It will then call the fetch_top_search_results function and display the top 10 search results along with their ranking.

Step 4: Running the Script Save the Python script with a descriptive filename, such as google_search.py. Open your terminal or command prompt, navigate to the folder where the script is saved, and execute it by running:

python google_search.py

Here’s the Updated Code:

from googlesearch import search

def fetch_top_search_results(query, num_results=10):
search_results = search(query, num_results=num_results)
return search_results

if __name__ == "__main__":
search_query = input("Enter your search query: ")
top_results = fetch_top_search_results(search_query, num_results=10)

print("Top 10 search results:")
for idx, result in enumerate(top_results, 1):
print(f"{idx}. {result}")
Here is the result after i searched
Enter your search query: sagarydv002
Top 10 search results:
1. https://www.instagram.com/sagarydv002/
2. https://in.linkedin.com/in/sagarydv002
3. https://medium.com/@sagarydv002/unveiling-the-indian-flag-a-technological-triumph-with-numpy-%EF%B8%8F-a4cba9881d9?responsesOpen=true&sortBy=REVERSE_CHRON
4. https://www.facebook.com/sagarydv002/
5. https://in.pinterest.com/sagarydv02/
6. https://brainly.in/question/22967224
7. https://git-stars.com/user/sagarydv002
8. https://in.pinterest.com/sagarydv02/
9. https://brainly.in/question/22967224
10. https://git-stars.com/user/sagarydv002

Conclusion: Python provides us with the tools to interact with various APIs and services, and Google Search is no exception. In this blog, we’ve explored how to use the googlesearch-python module to perform Google searches programmatically and fetch the top search results. With this knowledge, you can build more advanced applications like data scraping, sentiment analysis, and more.

Remember to use this functionality responsibly and be aware of the terms of service of the search engine you are interacting with. Happy coding and happy searching!

--

--