Google Search API: How to use Google Custom Search API

Google Search API: How to use Google Custom Search API

October 21, 2024 (1mo ago)

How to Use Google Custom Search API

In this blog post, we'll walk through the process of setting up and using the Google Custom Search API. This powerful tool allows you to create a customized search experience for your website or application. Let's get started!

Step 1: Create a Project in Google Cloud Console

  1. Go to https://console.cloud.google.com/
  2. Create a new project or select an existing one

Step 2: Enable the Custom Search API

  1. In the Google Cloud Console, navigate to the "Library" section
  2. Search for "Custom Search API"
  3. Click on the "Custom Search API" result
  4. Click the "Enable" button

Step 3: Create API Credentials

  1. In your project dashboard, go to the "Credentials" section
  2. Click "Create Credentials" and select "API Key"
  3. Copy and save your API key for later use

Step 4: Set Up a Programmable Search Engine

  1. Go to https://programmablesearchengine.google.com/controlpanel/all
  2. Click "Add" to create a new search engine
  3. Follow the prompts to set up your search engine
  4. Once created, copy your Search Engine ID for later use

Step 5: Install Required Library

Before using the API in your code, you need to install the required library. Open your terminal or command prompt and run the following command:

pip install requests

This will install the 'requests' library, which is used in the code example to make HTTP requests to the Google Custom Search API.

Step 6: Use the API in Your Code

Now that you have your API Key, Search Engine ID, and the required library installed, you can use them in your code. Here's a Python example that demonstrates how to use the Google Custom Search API:

import re
import requests
 
def build_payload(query, start=1, num=10, date_restrict='m1', **params):
    """
    Function to build the payload for the Google Search API request.
    
    :param query: Search term
    :param start: The index of the first result to return
    :param num: Number of results to return
    :param date_restrict: Restricts results based on recency (default is one month 'm1')
    :param params: Additional parameters for the API request
    
    :return: Dictionary containing the API request parameters
    """
    payload = {
        'key': API_KEY,
        'q': query,
        'cx': SEARCH_ENGINE_ID,
        'start': start,
        'num': num,
        'dateRestrict': date_restrict
    }
    payload.update(params)
    return payload
 
def clean_filename(filename):
    """
    Function to clean up string to be used as a filename.
    
    :param filename: The original string to be cleaned up
    :return: Cleaned up string safe for use as a filename
    """
    filename = re.sub(r'[\\/*?:"<>|]', "", filename)
    return filename
 
def make_request(payload):
    """
    Function to send a GET request to the Google Search API and handle potential errors.
    
    :param payload: Dictionary containing the API request parameters
    :return: JSON response from the API
    """
    response = requests.get('https://www.googleapis.com/customsearch/v1', params=payload)
    if response.status_code != 200:
        raise Exception('Request failed')
    return response.json()
 
def main(query, result_total=10):
    """
    Main function to execute the script.
    """
    items = []
    reminder = result_total % 10
    if reminder > 0:
        pages = (result_total // 10) + 1
    else:
        pages = result_total // 10
 
    for i in range(pages):
        if pages == i + 1 and reminder > 0:
            payload = build_payload(query, start=(i+1)*10, num=reminder)
        else:
            payload = build_payload(query, start=(i+1)*10)
        response = make_request(payload)
        items.extend(response['items'])
    
    with open('result.txt', 'w', encoding='utf-8') as f:
        for item in items:
            f.write(f"Title: {item.get('title', 'N/A')}\n")
            f.write(f"Link: {item.get('link', 'N/A')}\n")
            f.write(f"Snippet: {item.get('snippet', 'N/A')}\n")
            f.write("\n---\n\n")
 
if __name__ == '__main__':
    API_KEY = 'your_api_key'
    SEARCH_ENGINE_ID = 'your_search_engine_id'
    search_query = 'Today Date'
    total_results = 10
    main(search_query, total_results)

To use this code:

  1. Replace 'your_api_key' with your actual API key
  2. Replace 'your_search_engine_id' with your actual Search Engine ID
  3. Modify the search_query and total_results variables as needed

This script will perform a search using your Custom Search Engine and save the results to a file named result.txt.

Conclusion

You've now learned how to set up and use the Google Custom Search API. This powerful tool can be integrated into your projects to provide customized search functionality. Remember to keep your API key secure and follow Google's usage guidelines and quotas.

Happy searching!