SerpApi Python Library

repository·master·Indexed 41 days ago

https://github.com/serpapi/serpapi-python

The official Python client for SerpApi.com, providing a wrapper to integrate search engine data from Google, Bing, Baidu, DuckDuckGo, Amazon, eBay, and others into Python applications, AI workflows, and RAG pipelines. Features include support for Google Lens image uploads, Markdown and HTML output for LLMs, and geographic targeting via gl, hl, and location parameters.

Tokens
32.5K
Snippets
121
Records
142
Agent score
51%

What's inside SerpApi

  1. Media, Apps, and Research search examples

    master

    This category provides examples for searching media platforms, mobile applications, and academic research tools using SerpApi. Available search categories include:

    • YouTube: Video search.
    • Google Images: Image search.
    • Google Lens: Image upload and visual search.
    • Google Scholar: Academic research and scholarly articles.
    • Google Play Store: Mobile application data.
  2. Parse Google Flights search results

    master

    When processing results from the google_flights engine, it is recommended to check for best_flights first, and fallback to other_flights if the former is empty.

    Key fields available in the itinerary objects include:

    • price: The total cost of the itinerary.
    • total_duration: The total travel time.
    • carbon_emissions: Carbon footprint data.
    • flights: A list of individual flight segments within an itinerary. Each segment contains details like departure_airport and arrival_airport (which include airport ids), as well as departure and arrival times.
  3. Use the `json_restrictor` parameter to filter API responses

    master

    The json_restrictor parameter allows you to request only specific JSON fields from SerpApi, reducing payload size and processing overhead. SerpApi filters the response on the server side before sending it to your program. This parameter works across all SerpApi engines. The value is a selector string that follows specific syntax for selecting top-level sections, array items, slices, and nested fields.

    results = client.search(
        engine="google",
        q="coffee",
        json_restrictor="organic_results[].{title, link}",
    )
  4. Combine Search Parameters and Request Options

    master

    When calling SerpApi methods, the client distinguishes between search parameters (sent to SerpApi) and request options (sent to requests).

    • Request Options: timeout, proxies, verify, stream, and cert are passed to the requests library.
    • Search Parameters: All other keyword arguments are sent to SerpApi as API parameters (e.g., engine, q, location).

    If you provide both a parameter dictionary and additional keyword arguments, the keyword arguments will update the dictionary. To prevent modifying your original dictionary, pass a copy using .copy().

    # Combining parameters and options
    results = client.search(
        engine="google",
        q="coffee",
        location="Austin, Texas",
        timeout=10,
    )
    
    # Updating a dictionary with keyword arguments
    params = {"engine": "google", "q": "coffee"}
    results = client.search(params, location="Austin, Texas", timeout=10)
    
    # Using a copy to keep the original dictionary unchanged
    params = {"engine": "google", "q": "coffee"}
    results = client.search(params.copy(), location="Austin, Texas")
  5. Accessing search results in Python

    master

    When processing SerpApi responses in Python, use the .get() method on the results dictionary to safely access data fields. This prevents KeyError exceptions if a specific section of the search results is missing.

    Common patterns include:

    • Accessing lists of results: results.get("organic_results", []) ensures you get an empty list if no organic results are found.
    • Accessing individual item fields: item.get("title") returns None if the field is missing from a specific result.
    • Limiting output: Use Python slicing (e.g., [:5]) to iterate over a subset of the results.
    # Example pattern for safe data extraction
    results = search.get()
    organic_results = results.get("organic_results", [])
    
    for item in organic_results[:5]:
        title = item.get("title")
        print(title)
  6. Understand Google Finance API response structure

    master

    The Google Finance API returns several key fields in the results dictionary:

    • summary: Contains high-level data like title and price.
    • knowledge_graph: An alternative location for title and price depending on the response.
    • graph: A list of values representing data points over the specified time window.
    • news_results: A list containing related news headlines, when available.

    To ensure robust parsing, it is recommended to check both summary and knowledge_graph for identity and pricing information.

  7. Choose between serpapi.search() and serpapi.Client

    master

    Using serpapi.search()

    Use the top-level serpapi.search() function for quick, single searches. You must provide the api_key in every call.

    import serpapi
    
    results = serpapi.search(
        api_key="secret_api_key",
        engine="google",
        q="coffee",
    )

    Using serpapi.Client

    Create a serpapi.Client instance when you need to:

    • Make multiple requests (reuses the API key and HTTP session).
    • Fetch multiple pages of results.
    • Run searches concurrently.
    • Set a consistent timeout across all requests.
    import serpapi
    
    client = serpapi.Client(api_key="secret_api_key", timeout=20)
    results = client.search(engine="google", q="coffee")
  8. When to use asynchronous searches

    master

    Use Cases

    Use asynchronous searches when your application architecture separates the submission of searches from the collection of results. A typical pattern is a scheduled job that submits a batch of searches, which are then processed by a separate worker process later.

    Constraints

    • Do not combine async=True with no_cache=True.
    • Each Python client call is still a blocking HTTP request; the library does not use asyncio or await for these methods.
  9. Compare Zero Trace, Cache, and Async modes

    master

    Depending on your goal, you should use different parameters. Note that while the API documentation uses lowercase true, the Python library requires the boolean True with a capital T.

    GoalParameterNote
    Fetch fresh resultsno_cache=TrueBypasses cached responses
    Submit now and fetch laterasync=TrueUse in conjunction with search_archive()
    Skip storing search on serverszero_trace=TrueRequires account support
  10. Explore Google Trends data types

    master

    The google_trends engine supports several data_type options to change the shape of the returned data:

    • TIMESERIES: Returns search interest over time (found in interest_over_time.timeline_data).
    • GEO_MAP_0: Used to compare search interest across different regions.
    • RELATED_QUERIES: Returns queries related to your search term.
    • RELATED_TOPICS: Returns topics related to your search term.
  11. Read Bing organic search results

    master

    When searching Bing, web listings are typically found in the organic_results key of the response dictionary. Each listing in this list can contain the following fields:

    • title: The title of the search result.
    • link: The URL of the search result.
    • snippet: A brief description or excerpt from the page.
    • position: The numerical position of the result in the search list.

    You can inspect all available sections in a response by printing the dictionary keys: print(results.keys()).

    # To see which other sections the response contains
    print(results.keys())