SerpApi Python Client

1.0.2 · active · verified Tue Apr 14

The official Python client for SerpApi.com, allowing users to access structured search results from various engines like Google, Bing, Baidu, and more. Currently at version 1.0.2, the library is actively maintained with frequent updates and bug fixes, reflecting continuous development and responsiveness to API changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to perform a Google search using the `serpapi.Client`. It fetches results for 'Coffee' in Austin, Texas, and prints the title of the first organic result. The API key is securely retrieved from an environment variable.

import os
from serpapi import Client

# Ensure SERPAPI_KEY is set as an environment variable
# Example (bash): export SERPAPI_KEY='your_private_api_key'
api_key = os.environ.get('SERPAPI_KEY', '')

if not api_key:
    print("Error: SERPAPI_KEY environment variable not set.")
else:
    client = Client(api_key=api_key)
    try:
        results = client.search({
            "engine": "google",
            "q": "Coffee",
            "location": "Austin, Texas, United States",
            "hl": "en",
            "gl": "us"
        })
        print(results.get("organic_results", [])[0].get("title"))
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →