Small Python package that calls the Crawlbase Crawling API for a Google Search URL in AI Mode (udm=50), then normalizes the Crawlbase JSON into a stable shape: prompt, response_text, citations, and links.
Repository: github.com/ScraperHub/how-to-scrape-google-ai-mode-in-2026
- Python 3.10+
- A Crawlbase account and a regular Crawling API token (set as
CRAWLBASE_REGULAR_TOKENin.env)
git clone https://github.com/ScraperHub/how-to-scrape-google-ai-mode-in-2026.git
cd how-to-scrape-google-ai-mode-in-2026Use the directory that contains requirements.txt and the google_ai_mode package (in this layout that is the code folder):
cd code
python -m venv .venvActivate the virtual environment:
- Windows (PowerShell):
.venv\Scripts\Activate.ps1 - macOS / Linux:
source .venv/bin/activate
Install dependencies:
pip install -r requirements.txt-
In the Crawlbase dashboard, copy your normal / regular token for the Crawling API.
-
Copy the example env file and edit
.env(same folder asrequirements.txt):cp .env.example .env
On Windows (Command Prompt or PowerShell):
copy .env.example .env -
Set
CRAWLBASE_REGULAR_TOKENto that value.
Do not commit .env; it is listed in .gitignore. Rotate any token that was committed or pasted in public chat.
From the directory that contains requirements.txt (and with the venv activated):
python -m google_ai_mode "your search query"JSON is printed to stdout (pretty-printed). Redirect if you want a file:
python -m google_ai_mode "your search query" > output.jsonYou can pass the query via GOOGLE_AI_MODE_QUERY in .env instead of a positional argument, then run:
python -m google_ai_mode| Option | Description |
|---|---|
query |
Positional search string (optional if GOOGLE_AI_MODE_QUERY is set). |
--gl |
Google gl parameter (default: us). |
--hl |
Google hl parameter (default: en). |
--no-scraper |
Omit Crawlbase scraper=google-serp (default is to send it). |
The top-level object has a results array with one item. Useful fields include:
results[0].content:prompt,response_text,citations,links,parse_status_coderesults[0].url: Google AI Mode URL that was requestedresults[0].status_code,pc_status,crawl_url,token_used,scraperresults[0].raw_body_preview: Short preview of the rawbodyfrom Crawlbase (for debugging parsers)
With PYTHONPATH or from an environment where the package is installed:
from google_ai_mode import scrape_google_ai_mode
data = scrape_google_ai_mode("example query", gl="us", hl="en")Set CRAWLBASE_REGULAR_TOKEN…— Add the variable to.envor export it in your shell, then rerun from the same directory sopython-dotenvcan loadcode/.env(when the package lives undercode/).401/ Crawlbase errors — Confirm the token type matches a regular Crawling API token and that billing or limits are not blocking requests.- Sparse
response_text/ oddcitations— Inspectraw_body_previewand Crawlbase’sbody; Google and Crawlbase payloads change over time. Adjust parsing ingoogle_ai_mode/normalize.pyif needed.
Use and modify this sample in line with your repository’s license and with applicable laws and third-party terms for accessing search results.