DEV Community

Swapnil J
Swapnil J

Posted on

How to Extract Google Search Results Using Python and a SERP API

Getting Google search results with Python sounds like a simple task until you need to collect them consistently.

A basic scraper might work for a few requests, but things become more complicated when you encounter CAPTCHAs, blocked requests, changing HTML structures and proxy management.

This is where a SERP API can be useful.

What is a SERP API?

A Search Engine Results Page (SERP) API allows developers to retrieve structured search results without having to build and maintain their own search scraping infrastructure.

Some practical use cases include:

  • SEO and keyword rank tracking
  • Competitor monitoring
  • Market research
  • AI agents and applications
  • Automated data collection

In this tutorial, we'll use OrbitScraper, a SERP API that our team is building for developers.

Step 1: Install the required Python library

pip install requests
Enter fullscreen mode Exit fullscreen mode

Step 2: Get your API key

Create an account at OrbitScraper and generate an API key from your dashboard.

Store your API key in an environment variable named ORBIT_API_KEY rather than hardcoding it in your application.

Step 3: Send a search request

import os
import requests
import time

BASE_URL = "https://api.orbitscraper.com"

headers = {
    "x-api-key": os.environ["ORBIT_API_KEY"],
    "Content-Type": "application/json"
}

payload = {
    "q": "best AI tools for developers",
    "location": "United States",
    "gl": "us",
    "hl": "en",
    "num": 10
}

response = requests.post(
    f"{BASE_URL}/v1/search",
    headers=headers,
    json=payload,
    timeout=30
)

response.raise_for_status()
job_id = response.json()["jobId"]
Enter fullscreen mode Exit fullscreen mode

Step 4: Retrieve the search results

Once the search job has been created, check its status and retrieve the results when it is completed.

for _ in range(60):
    response = requests.get(
        f"{BASE_URL}/v1/search/{job_id}",
        headers=headers,
        timeout=30
    )

    response.raise_for_status()
    data = response.json()

    if data["status"] == "completed":
        for result in data["result"]["organic_results"]:
            print(result)
        break

    if data["status"] in ("failed", "expired"):
        raise RuntimeError(data["status"])

    time.sleep(1)
else:
    raise TimeoutError("Search polling limit reached")
Enter fullscreen mode Exit fullscreen mode

You can now use the structured results in your Python application, store them in a database or pass them into an analytics workflow.

Where can you use this?

One interesting use case is building an automated keyword rank tracker.

Instead of manually searching for keywords every day, you can schedule search requests, store organic results and compare ranking changes over time.

The same approach can be used to build competitor monitoring tools, research applications and AI agents that require access to current search data.

Why we built OrbitScraper

We wanted developers to spend more time building their applications and less time maintaining search scraping infrastructure.

OrbitScraper offers:

  • Structured search data
  • JSON and Markdown responses
  • MCP integration for AI applications
  • Credit-based pricing
  • Monthly and yearly subscriptions
  • Non-expiring top-up credits

You can explore the documentation here:

https://orbitscraper.com/docs

I'd also love to hear from other developers in this community.

If you're currently collecting search engine data, are you maintaining your own scraping infrastructure or using a managed SERP API? What has been your biggest challenge so far?

Top comments (0)