SERP API Python Tutorial: Scrape Google Results
Learn how to use Searlo's Google SERP API with Python. This guide covers basic search queries, geo-targeting, rank tracking, SERP feature extraction, and AI/LLM integration.
Prerequisites
- • Python 3.6 or higher installed
- • Free Searlo account (sign up here)
- • Basic understanding of Python and REST APIs
Quick Start Steps
Get Your API Key
Sign up for free at dashboard.searlo.tech and copy your API key from the settings page.
Install Dependencies
Install the requests library for making HTTP calls.
Make Your First Request
Use the search endpoint to query Google and get structured results.
Parse the Response
Extract organic results, featured snippets, and other SERP features.
Build Your Application
Use the data for rank tracking, SEO analysis, or AI applications.
1. Installation
Install the requests library if you don't have it already:
pip install requests2. Basic Google Search
Here's a simple function to search Google and get structured results:
import requests
API_KEY = "your_api_key_here"
BASE_URL = "https://api.searlo.tech/api/v1"
def search_google(query, num_results=10):
"""Search Google using Searlo API"""
response = requests.get(
f"{BASE_URL}/search/web",
headers={"x-api-key": API_KEY},
params={
"q": query,
"limit": num_results,
}
)
return response.json()
# Example usage
results = search_google("best python frameworks 2026")
for result in results.get("organic", []):
print(f"{result['position']}. {result['title']}")
print(f" {result['link']}")3. Geo-Targeted Search
Search from specific countries or cities to get localized results:
def search_localized(query, country="us", language="en"):
"""Search with geo-targeting"""
response = requests.get(
f"{BASE_URL}/search/web",
headers={"x-api-key": API_KEY},
params={
"q": query,
"gl": country, # Country code
"hl": language, # Language
}
)
return response.json()
# Search from UK
uk_results = search_localized("coffee shops", country="uk")4. Build a Rank Tracker
Track your domain's position for multiple keywords:
import json
from datetime import datetime
def track_rankings(keywords, domain):
"""Track domain rankings for multiple keywords"""
rankings = {}
for keyword in keywords:
results = search_google(keyword, num_results=100)
position = None
for result in results.get("organic", []):
if domain in result.get("link", ""):
position = result["position"]
break
rankings[keyword] = {
"position": position,
"date": datetime.now().isoformat(),
"keyword": keyword
}
return rankings
# Track rankings for your domain
keywords = ["serp api", "google search api", "web scraping api"]
my_rankings = track_rankings(keywords, "searlo.tech")
print(json.dumps(my_rankings, indent=2))5. Pagination and Spelling Metadata
Every response includes result counts, page info, and spelling suggestions alongside the organic results:
def search_with_metadata(query):
"""Pull organic results plus pagination and spelling metadata"""
results = search_google(query)
summary = {
"total_results": results.get("totalResults", 0),
"current_page": results.get("page", 1),
"has_next_page": results.get("nextPage") is not None,
"organic_count": len(results.get("organic", [])),
"spelling_suggestion": results.get("spelling", {}).get("suggested"),
}
return summary
# Inspect a query's metadata before deciding how many pages to pull
info = search_with_metadata("what is machine learning")
print(f"{info['organic_count']} results on page {info['current_page']}")
if info["spelling_suggestion"]:
print("Did you mean:", info["spelling_suggestion"])6. AI/LangChain Integration
Use Searlo with LangChain for AI agent applications:
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
class SearloSearchTool:
"""LangChain-compatible search tool"""
def __init__(self, api_key):
self.api_key = api_key
def search(self, query: str) -> str:
response = requests.get(
"https://api.searlo.tech/api/v1/search",
headers={"x-api-key": self.api_key},
params={"q": query, "num": 5, "format": "toon"}
)
return response.text
# Use with LangChain
search_tool = Tool(
name="Web Search",
func=SearloSearchTool(API_KEY).search,
description="Search the web for current information"
)
agent = initialize_agent(
tools=[search_tool],
llm=OpenAI(temperature=0),
agent="zero-shot-react-description"
)
result = agent.run("What are the latest AI developments?")Frequently Asked Questions
What Python version is required?
Searlo's API works with Python 3.6+. We recommend Python 3.9 or later for the best experience.
Is there an official Python SDK?
A lightweight official SDK is on the way. Until it ships, the raw `requests` calls in this guide work today and are what the SDK will wrap internally — no need to wait to get started.
How do I handle rate limits?
Searlo returns a 429 status code when rate limited. Implement exponential backoff or use our async endpoints for high-volume requests.
Can I use this for commercial projects?
Absolutely! Searlo is designed for production use. Our paid plans include commercial licensing and SLA guarantees.
Ready to start building?
Get 3,000 free credits to start. No credit card required.