As a developer, you may often need to programmatically fetch information from search engines like Google. This is where the Google Search API comes into play. The Google Search API for Python provides a simple, easy-to-use way to search the web and retrieve relevant results. In this beginner’s guide, we will explore how to use the Google Search API in Python.

We will cover the necessary prerequisites, such as setting up a Google API key, and then dive into the code, starting with simple search queries and gradually moving on to more advanced search features. One of the key advantages of using the Google Search API is the ability to customize search queries to filter and sort search results.

Whether you’re a beginner or an experienced Python developer, this guide will provide the essential skills to use the Google Search API in your projects. So, let’s get started!

Google search API python or Google custom search API for google api client

What Are the Prerequisites to Using Google Search API in Python Code?

Before we perform any task, there is always some important prerequisite to it. Here are some prerequisites for using the Google search console API in Python.

Python Installation

Before you can start using the Google Search API, you will need to have Python installed on your system. You can download and install Python from the official Python website (

Installing Required Packages

You will also need to install the required Python packages to use the Google Search API. The most important package is the google-api-python-client, which can be installed using pip (Python package installer) by running the following command in your terminal or command prompt:

pip install google-api-python-client

Creating a Google API Key

Finally, you will need to create a Google API key to use the Google Search API. To do this, you will need a Google account. Once you have a Google account, you can go to the Google Cloud Console ( and create a new project.

Then, you can enable the Google Search API for your project and create an API key. You will need to provide this API key when making requests to the Google Search API.

How Do Get Started With the Google Search API in Python?

After you are done completing the above steps, you can now get started with Google Search API. Here are further steps:

Importing Necessary Packages

You will need to import the necessary packages in your Python code to use the Google Search API. The most important package is the googleapiclient package, which provides the main interface for interacting with the Google Search API.

You can also import other packages like json and os for handling the API response and API key, respectively. Here is an example of how to import these packages:

import os
import json
from googleapiclient.discovery import build

Authenticating the API Key

Before you can make requests to the Google Search API, you will need to authenticate your API key. You can do this by creating a service object using the build function from the googleapiclient package.

You will need to pass in the name of the API you want to use (in this case, “customsearch”) and your API key. Here is an example of how to authenticate your API key:

api_key = "YOUR_API_KEY"
service = build("customsearch", "v1", developerKey=api_key)

Initializing the Search Engine

Next, you will need to initialize the search engine you want to use. You can do this by specifying the search engine ID and the API version. The search engine ID is a unique identifier for the search engine you want to use.

You can create a custom search engine in the Google Custom Search Engine (CSE) console and get its ID from there. Here is an example of how to initialize the search engine:

search_engine_id = "YOUR_SEARCH_ENGINE_ID"
api_version = "v1"
search_engine = service.cse().list(cx=search_engine_id, version=api_version)

Running a Basic Search Query

Finally, you can run a basic search query by specifying the search query string and calling the execute method on the search engine object. The response from the API will be a JSON object containing the search results. Here is an example of how to run a basic search query.

query = "zenserp API tutorials"
search_engine.q = query
response = search_engine.execute()
results = response["items"]

Google showing first ten search results in user interface

How to Customise Search Queries When Using Google Search API in Python?

Here are some of the most useful search parameters that you can use to customize your search queries when using the Google Search API in Python:

  1. q (required): Specifies the search query string. This parameter is required in every search request.
  2. num: Specifies the number of search results to return. The maximum value is 10, but you can use pagination to retrieve more results.
  3. start: Specifies the index of the first search result to return. This parameter is useful for pagination.
  4. siteSearch: Limits the search to a specific website or domain.
  5. siteSearchFilter: Controls whether to include or exclude search results from the specified website or domain.
  6. fileType: Limits the search to a specific file type (e.g., PDF, DOC, XLS).
  7. dateRestrict: Limits the search results to a specific date range (e.g., past year, past month, custom range).
  8. sort: Specifies the order in which to sort the search results (e.g., by relevance, by date).
query = "python tutorials"
search_engine.q = query
search_engine.num = 10
search_engine.start = 1
search_engine.siteSearch = ""
search_engine.siteSearchFilter = "i"
search_engine.fileType = "pdf"
search_engine.dateRestrict = "d1" # results from past day
search_engine.sort = "date" # sort by date
response = search_engine.execute()
results = response["items"]

Can We Use Zenserp API in Python To Scrap Google Search Results?

Zenserp is one of the most powerful APIs that you can use for scraping Google search results. Let’s learn about it first.


Zenserp application programming interface is a web scraping service that provides an API to extract search engine results from Google, Bing, Yandex, and other search engines. It enables developers to retrieve structured search engine data in a variety of formats, including JSON, CSV, and HTML.

Zenserp custom search engine id to scrape entire web in python library

Zenserp provides a range of features to help developers with their web scraping needs. It includes support for multiple languages, advanced search queries, and the ability to bypass CAPTCHAs. It also offers an SDK for a range of programming languages, including Python, Java, and PHP.

Here is a sample code on how you can scrape Google search results in Python through Zenserp:

import requests

headers = { 
  "apikey": "YOUR-APIKEY"}

params = (
   ("q","Pied Piper"),
   ("location","New York,New York,United States"),

response = requests.get('', headers=headers, params=params);

Final Thoughts

Google Search API is a powerful tool for developers to access search engine results and extract useful information from them. This beginner’s guide has provided a step-by-step approach to getting started with the Google Search API in Python, including the installation of necessary packages, authentication, and running basic search queries.

Additionally, we have covered how to customize search queries by using parameters such as num, start, siteSearch, fileType, dateRestrict, and sort. Finally, we have discussed the possibility of using Zenserp API in Python to scrape Google search results, such as Google images.


Is There a Google Search API for Python?

You can use it in Python by following the steps given above.

How to Use Google Search API in Python?

You have to prepare some prerequisites, import packages, and start using your API in Python. Find further details in the above guide.

Is Google Search API Free?

Google Search API offers both free and paid plans. The free plan is known as the Custom Search JSON API and allows up to 100 queries per day. However, the free plan has certain limitations, such as the inability to remove ads from search results, and it also does not provide access to certain advanced features.

Is There a Google API for Search?

Yes, Google offers several APIs for search, which allow developers to integrate Google search functionality into their applications.

Sign Up today to start scraping Google search results easily by implementing our API in your application – Zenserp API.