# Get Google search results with Python and save them to CSV

Send a Google Search API request, read organic results and export titles, URLs and snippets with Python’s standard library.

Canonical reference: https://flashdata.dev/blog/google-search-python
Blog: Google Search · Python · FlashData
Published: 2026-09-17
RSS: https://flashdata.dev/blog/rss.xml

<a id="prepare"></a>

## Prepare your environment

You will use Python 3 and its standard library. Create an [API key](https://flashdata.dev/app/api-keys) and make sure your account has available credits. If this is your first request, see the [API quickstart](https://flashdata.dev/docs/sources/google_search/reference.md#quickstart).

```bash
# Replace YOUR_API_KEY near the top of the code below.
```

The script below searches for `openai` using US results and an English interface, then writes the returned organic links to a CSV file.

<a id="request"></a>

## Fetch and save the search results

Save the script as `search.py`. It submits one realtime request with `source: "google_search"` and reads the `organic` array from the first result.

```python
import csv
import json
import os
import urllib.request

base_url = "https://data.flashdata.dev"
api_key = "YOUR_API_KEY"
request = urllib.request.Request(
    base_url + "/v1/queries/realtime",
    data=json.dumps({
        "source": "google_search",
        "query": "openai",
        "params": {"gl": "us", "hl": "en", "num": 10},
    }).encode("utf-8"),
    headers={
        "X-API-Key": api_key,
        "Content-Type": "application/json",
    },
    method="POST",
)

with urllib.request.urlopen(request, timeout=60) as response:
    payload = json.load(response)

if payload.get("status") != "done":
    raise RuntimeError(payload.get("message", "Search did not complete"))

results = payload["results"][0].get("organic", [])
with open("google-results.csv", "w", newline="", encoding="utf-8") as output:
    writer = csv.DictWriter(output, fieldnames=["position", "title", "url", "snippet"])
    writer.writeheader()
    for result in results:
        writer.writerow({
            "position": result.get("position", ""),
            "title": result.get("title", ""),
            "url": result.get("link", ""),
            "snippet": result.get("snippet", ""),
        })

print(f"Saved {len(results)} results to google-results.csv")
```

```bash
python3 search.py
```

Open `google-results.csv` in a spreadsheet or load it into your application. The exported columns are position, title, URL and snippet. Python’s CSV writer handles quoting for commas and line breaks inside result text.

<a id="response"></a>

## Understand the API response

This recorded response shows the structure returned for an OpenAI search. The script exports organic links; other available sections remain in the API response if your application needs them.

```json
{
  "source": "google_search",
  "status": "done",
  "results": [
    {
      "searchParameters": {
        "q": "openai",
        "gl": "us",
        "hl": "en",
        "type": "search",
        "num": 10,
        "page": 1,
        "engine": "google"
      },
      "organic": [
        {
          "title": "OpenAI | Research & Deployment",
          "link": "https://openai.com/",
          "snippet": "We believe our research will eventually lead to artificial general intelligence, a system that can solve human-level problems.",
          "position": 1
        },
        {
          "title": "OpenAI",
          "link": "https://en.wikipedia.org/wiki/OpenAI",
          "snippet": "OpenAI is an American artificial intelligence (AI) public benefit corporation (PBC) headquartered in San Francisco. It develops proprietary generative AI models ...",
          "position": 2
        },
        {
          "title": "OpenAI",
          "link": "https://x.com/OpenAI",
          "snippet": "OpenAI's mission is to ensure that artificial general intelligence benefits all of humanity. can develop research, build financial models, and create ...",
          "position": 3
        },
        {
          "title": "OpenAI",
          "link": "https://www.linkedin.com/company/openai",
          "snippet": "OpenAI is an AI research and deployment company dedicated to ensuring that general-purpose artificial intelligence benefits all of humanity.",
          "position": 4
        },
        {
          "title": "r/OpenAI",
          "link": "https://www.reddit.com/r/OpenAI/",
          "snippet": "OpenAI is an AI research and deployment company. OpenAI&#39;s mission is to create safe and powerful AI that benefits all of humanity. OpenAI's ...",
          "position": 5
        }
      ],
      "peopleAlsoAsk": [
        {
          "question": "Is OpenAI created by Elon Musk?"
        },
        {
          "question": "Why are people leaving ChatGPT?"
        },
        {
          "question": "Is OpenAI the same as ChatGPT?"
        },
        {
          "question": "Why is OpenAI in trouble?"
        }
      ],
      "relatedSearches": [
        {
          "query": "OpenAI login"
        },
        {
          "query": "OpenAI chat"
        },
        {
          "query": "OpenAI ChatGPT"
        },
        {
          "query": "OpenAI - wikipedia"
        },
        {
          "query": "OpenAI API"
        },
        {
          "query": "OpenAI app"
        },
        {
          "query": "OpenAI download"
        },
        {
          "query": "OpenAI video"
        }
      ]
    }
  ],
  "usage": {
    "credits": 0.02
  },
  "created_at": "2026-09-16T07:41:40.043Z"
}
```

The `num` parameter requests a result count. It does not guarantee that Google will return that many organic links. The script saves however many results are returned.

<a id="next"></a>

## Change the search

Change `query` for your topic, `params.gl` for the country and `params.hl` for the interface language. Use `params.page` to request another page; each page is a separate API call.

To compare results over time, store the query, region and collection time alongside each row. The API returns a search snapshot; historical comparisons come from the snapshots you save.

## Related guides

- [All articles](https://flashdata.dev/blog/reference.md)
- [Measure API latency and download throughput](https://flashdata.dev/docs/guides/performance/reference.md)
- [Google Search API](https://flashdata.dev/products/google-search/reference.md)
- [Compare plans and rates](https://flashdata.dev/pricing/google-search/reference.md)
- [Compare providers](https://flashdata.dev/compare/reference.md)
- [Google Search JSON APIs in 2026: which one to use](https://flashdata.dev/blog/google-search-json-api/reference.md)
- [SerpAPI alternatives for Google search JSON](https://flashdata.dev/blog/serpapi-alternatives/reference.md)
