Python developer guide

Google Search API in Python

A complete Python guide to PrismCrawl's Google Search API: authentication, parameters, pagination, location, JSON parsing, timeouts, and errors.

Install and authenticate

Create a free PrismCrawl account, copy the API key from your dashboard, and store it in an environment variable. The examples use requests; the API works with any HTTP client. Start with the Google Search API overview for supported capabilities, or compare the JavaScript implementation.
terminal
python -m pip install requests
export PRISMCRAWL_API_KEY="replace-with-your-key"

Complete working request

search_google.py
import os
import requests

API_URL = "https://api.prismcrawl.com/v1/google/search"

def search_google(query: str, **parameters):
    response = requests.post(
        API_URL,
        headers={
            "x-api-key": os.environ["PRISMCRAWL_API_KEY"],
            "content-type": "application/json",
        },
        json={"query": query, **parameters},
        timeout=30,
    )
    response.raise_for_status()
    payload = response.json()
    if not payload.get("success"):
        raise RuntimeError(payload.get("error", "Search failed"))
    return payload["data"]["content"]

data = search_google(
    "best coffee shops",
    gl="us",
    hl="en-US",
    location="Austin,Texas,United States",
    device="mobile",
)

for item in data.get("results", []):
    print(f'{item["rank"]:>2}  {item["title"]}')
    print(f'    {item["url"]}')

Or use the official Python SDK

The prismcrawl package on PyPI wraps every endpoint with type hints, automatic retries, page iterators, and an async client. It reads PRISMCRAWL_API_KEY from the environment. Source and examples are on GitHub. For a larger example with pagination and the async client, see scraping Google Maps, Yelp, and Tripadvisor reviews with Python.
search_google_sdk.py
# pip install prismcrawl
import prismcrawl
from prismcrawl import PrismCrawl

client = PrismCrawl()  # retries 429s, 5xx, and connection errors

try:
    response = client.google.search(
        query="best coffee shops",
        gl="us",
        location="Austin,Texas,United States",
    )
except prismcrawl.APIStatusError as error:
    print(error.status_code, error.code, error.message, error.request_id)
    raise

for item in response["data"]["content"]["results"]:
    print(item["rank"], item["title"], item["url"])

Parameters that change the SERP

  • query: the Google search expression
  • gl: result country signal
  • hl: Google interface language
  • location or coordinates plus radius
  • device: desktop, tablet, or mobile
  • start offset, search tab, and supported native filters

Pagination

Do not assume every query exposes the same number of pages. Check the response before requesting the next page.
pagination.py
start = 0
while True:
    data = search_google("site:example.com product", gl="us", start=start)
    for item in data.get("results", []):
        print(item["rank"], item["url"])
    if not data.get("has_next_page"):
        break
    start += 10

Errors and production safeguards

  • Set an explicit client timeout
  • Call raise_for_status() for HTTP failures
  • Treat 401/403 as key or authorization failures
  • Retry 429 and transient 5xx responses with capped exponential backoff
  • Log the PrismCrawl request_id for support and debugging
  • Do not retry validation errors without changing parameters

Parse features separately

Organic results and serp_features are separate so feature-specific logic does not distort rank readers. Inspect actual response schemas in the API documentation, try a request in the playground, and review SERP API capabilities and pricing.

Make your first live search today.

Create an account, get 100 free credits, and test live Google or Bing results. No credit card or subscription required.