Skip to main content

Official Python SDK for Serpex Search API - Fetch search results in JSON format

Project description

serpex

Official Python SDK for the Serpex SERP API - Fetch search results in JSON format.

Installation

pip install serpex

Or with poetry:

poetry add serpex

Quick Start

from serpex import SerpexClient

# Initialize the client with your API key
client = SerpexClient('your-api-key-here')

# Search with auto-routing (recommended for simple use cases)
results = client.search({
    'q': 'python tutorial',
    'engine': 'auto'
})

# Or using SearchParams object for type safety
from serpex import SearchParams

params = SearchParams(q='python tutorial', engine='auto')
results = client.search(params)

print(results.results[0].title)

API Reference

SerpexClient

Constructor

SerpexClient(api_key: str, base_url: str = "https://api.serpex.dev")
  • api_key: Your API key from the Serpex dashboard
  • base_url: Optional base URL (defaults to 'https://api.serpex.dev')

Methods

extract(params: ExtractParams | Dict[str, Any]) -> ExtractResponse

Extract content from web pages and convert them to LLM-ready markdown data. Accepts up to 10 URLs per request.

# Using dictionary (simple approach)
results = client.extract({
    'urls': [
        'https://example.com',
        'https://httpbin.org'
    ]
})

# Using ExtractParams object (type-safe approach)
from serpex import ExtractParams

params = ExtractParams(urls=[
    'https://example.com',
    'https://httpbin.org'
])
results = client.extract(params)

Extract Parameters

The ExtractParams dataclass supports extraction parameters:

@dataclass
class ExtractParams:
    # Required: URLs to extract (max 10)
    urls: List[str]

Extract Response Format

@dataclass
class ExtractResponse:
    success: bool
    results: List[ExtractResult]
    metadata: ExtractMetadata

@dataclass
class ExtractResult:
    url: str
    success: bool
    markdown: Optional[str] = None
    error: Optional[str] = None
    status_code: Optional[int] = None

@dataclass
class ExtractMetadata:
    total_urls: int
    processed_urls: int
    successful_crawls: int
    failed_crawls: int
    credits_used: int
    response_time: int
    timestamp: str

Search Parameters

The SearchParams dataclass supports all search parameters:

@dataclass
class SearchParams:
    # Required: search query
    q: str

    # Optional: Engine selection (defaults to 'auto')
    engine: Optional[str] = 'auto'

    # Optional: Search category ('web' for general search, 'news' for news articles - always returns latest news)
    category: Optional[str] = 'web'  # Supports: 'web', 'news'

    # Optional: Time range filter (only applicable for 'web' category, ignored for 'news')
    time_range: Optional[str] = 'all'

    # Optional: Response format
    format: Optional[str] = 'json'

News Search Example

News search always returns the latest news articles. The time_range parameter is ignored for news searches.

# Search for latest news articles
news_results = client.search({
    'q': 'artificial intelligence',
    'engine': 'google',
    'category': 'news'  # Always returns latest news
})

print(news_results.results[0].title)
print(news_results.results[0].published_date)

## Supported Engines

- **auto**: Automatically routes to the best available search engine
- **google**: Google's primary search engine
- **bing**: Microsoft's search engine
- **duckduckgo**: Privacy-focused search engine
- **brave**: Privacy-first search engine
- **yahoo**: Yahoo search engine
- **yandex**: Russian search engine

## Response Format

```python
@dataclass
class SearchResponse:
    metadata: SearchMetadata
    id: str
    query: str
    engines: List[str]
    results: List[SearchResult]
    answers: List[Any]
    corrections: List[str]
    infoboxes: List[Any]
    suggestions: List[str]

Error Handling

The SDK raises SerpApiException for API errors:

from serpex import SerpexClient, SerpApiException

try:
    results = client.search(SearchParams(q='test query'))
except SerpApiException as e:
    print(f"API error: {e}")
    print(f"Status code: {e.status_code}")
    print(f"Details: {e.details}")

Examples

Basic Search

results = client.search({
    'q': 'coffee shops near me'
})

Advanced Search with Filters

results = client.search({
    'q': 'latest AI news',
    'engine': 'google',
    'time_range': 'day',
    'category': 'web'
})

Using SearchParams Object

from serpex import SearchParams

params = SearchParams(
    q='machine learning',
    engine='auto',
    time_range='month'
)
results = client.search(params)

Extract Web Content to LLM-Ready Data

Extract from a Single URL

# Extract content from one website
result = client.extract({
    'urls': ['https://example.com']
})

if result.results[0].success:
    print(f"✅ Extracted {len(result.results[0].markdown)} characters")
    print("Markdown content:", result.results[0].markdown[:200] + "...")

Extract from Multiple URLs (up to 10 at once)

# Extract content from multiple websites (up to 10 URLs)
extract_results = client.extract({
    'urls': [
        'https://example.com',
        'https://httpbin.org',
        'https://github.com'
    ]
})

print(f"Successfully extracted {extract_results.metadata.successful_crawls} pages")
print(f"Total credits used: {extract_results.metadata.credits_used}")

for result in extract_results.results:
    if result.success:
        print(f"✅ {result.url}: {len(result.markdown)} characters")
        # Use result.markdown for LLM processing
    else:
        print(f"❌ {result.url}: {result.error}")

Sample Response

# Example response structure
{
    'success': True,
    'results': [
        {
            'url': 'https://example.com',
            'success': True,
            'markdown': '# Example Domain\n\nThis domain is for use in...',
            'status_code': 200
        }
    ],
    'metadata': {
        'total_urls': 1,
        'processed_urls': 1,
        'successful_crawls': 1,
        'failed_crawls': 0,
        'credits_used': 3,
        'response_time': 255,
        'timestamp': '2025-11-13T10:30:00.000Z'
    }
}

Using ExtractParams Object

from serpex import ExtractParams

params = ExtractParams(urls=[
    'https://example.com',
    'https://httpbin.org'
])
results = client.extract(params)

Requirements

  • Python 3.8+
  • requests

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serpex-2.3.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serpex-2.3.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file serpex-2.3.0.tar.gz.

File metadata

  • Download URL: serpex-2.3.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serpex-2.3.0.tar.gz
Algorithm Hash digest
SHA256 346e566537a67bcc3b8bd6afbae088cc0630a01f9cd100cc4224b4993eaa406f
MD5 789cdb167b2c173406663527341af4da
BLAKE2b-256 93cd76d0159816d4a66d347450ded1bf9da04f7f4675534083064d225c36e63a

See more details on using hashes here.

File details

Details for the file serpex-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: serpex-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serpex-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28420e45b9f27cb1eadfe88d6cef1e32b58004d67cd48a8a80377b173b0d0546
MD5 70e975a06fb45677c74d37aae8b214d5
BLAKE2b-256 0b5bddbf3468c7fc766cd38f94c7ec44ee763966edf14932258358e8fc730406

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page