Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

a Python client library for SerpApi.

License

NotificationsYou must be signed in to change notification settings

serpapi/serpapi-python

 
 

Repository files navigation

Official Python client for SerpApi.com - Search Engine Results API.

Features

  • Async/await support for non-blocking HTTP requests
  • Persistent connections for 2x faster response times
  • Multiple search engines (Google, Bing, Yahoo, Baidu, Yandex, etc.)
  • Comprehensive API coverage (Search, Location, Account, Search Archive)
  • Type hints and extensive documentation

Installation

# Using pippip install serpapi# Using uv (recommended)uv add serpapi

Quick Start

importasynciofromserpapiimportClientasyncdefmain():client=Client(api_key="your_api_key",engine="google")results=awaitclient.search({"q":"coffee"})forresultinresults.get("organic_results", []):print(f"Title:{result.get('title')}")print(f"Link:{result.get('link')}")awaitclient.close()asyncio.run(main())

SerpApi documentation.

API Key

Get your API key fromserpapi.com/signup.

Set environment variable:

export SERPAPI_KEY="your_secret_key"

Documentations

This library is well documented, and you can find the following resources:

Usage Examples

Basic Search

results=awaitclient.search({"q":"coffee"})print(f"Found{len(results.get('organic_results', []))} organic results")

HTML Search

html_content=awaitclient.html({"q":"coffee"})print(f"HTML content length:{len(html_content)} characters")

Location API

locations=awaitclient.location({"q":"Austin","limit":3})print(f"Found{len(locations)} locations")

Search Archive

archived=awaitclient.search_archive(search_id)print(f"Retrieved archived search:{archived.get('search_metadata', {}).get('id')}")

Account Info

account=awaitclient.account()print(f"Account plan:{account.get('plan')}")

Async Batch Processing

importasyncioasyncdefsearch_company(client,company):results=awaitclient.search({"q":company})return {"company":company,"count":len(results.get("organic_results", []))}asyncdefmain():client=Client(api_key="your_api_key",persistent=True)companies= ["meta","amazon","apple","netflix","google"]tasks= [search_company(client,company)forcompanyincompanies]results=awaitasyncio.gather(*tasks)forresultinresults:print(f"{result['company']}:{result['count']} results")awaitclient.close()asyncio.run(main())

Context Manager

asyncwithClient(api_key="your_api_key")asclient:results=awaitclient.search({"q":"coffee"})# Client automatically closed

Error Handling

fromserpapiimportSerpApiErrortry:results=awaitclient.search({"q":"coffee"})exceptSerpApiErrorase:print(f"SerpApi error:{e}")

Developer guide

The UV Package Manager must be installed. Seeuv installation instructions.

The following commands are available:

# Install dependencies (including formatting tools)uv sync --dev# Run testsuv run pytest# Run test with coverageuv run pytest --cov=serpapi tests/# Type checking with mypyuv run mypy serpapi/# Format code with blackuv run black serpapi/# Sort imports with isortuv run isort serpapi/# Check formatting without making changesuv run black --check.uv run isort --check-only.

UV Benefits

  • Fast: 10-100x faster than pip
  • Reliable: Lock file ensures reproducible builds
  • Simple: Single command for most operations
  • Modern: Built for Python 3.11+ with async support

Project Structure with UV

serpapi-python/├── .python-version          # Python version (3.11)├── uv.lock                  # Dependency lock file├── .venv/                   # Virtual environment (auto-created)├── pyproject.toml           # Project configuration├── serpapi/                 # Package source code├── tests/                   # Test suite├── examples/                # Usage examples└── README.md               # This file

License

MIT License - see LICENSE file for details.


[8]ページ先頭

©2009-2025 Movatter.jp