### Quick Start Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Basic example of how to use the GoogleSearch class to perform a search and get results. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "location": "Austin,Texas", "api_key": "" }) result = search.get_dict() ``` -------------------------------- ### Quick start Source: https://github.com/serpapi/google-search-results-python/blob/master/README.rst Example of performing a search for 'coffee' using the GoogleSearch class. ```python from serpapi import GoogleSearch search = GoogleSearch({"q": "coffee", "location": "Austin,Texas", "api_key": "secretKey"}) result = search.get_dict() ``` -------------------------------- ### Get HTML Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example for retrieving search results in raw HTML format. ```python html = client.get_html() # Parse HTML with BeautifulSoup or similar ``` -------------------------------- ### Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/apple_app_store_search.md Example of how to instantiate the AppleAppStoreSearch client with search parameters. ```python from serpapi import AppleAppStoreSearch # Basic search (note: use term parameter) search = AppleAppStoreSearch({ "term": "ipad", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Installation Source: https://github.com/serpapi/google-search-results-python/blob/master/README.rst Install the google-search-results package using pip. ```shell pip install google-search-results ``` -------------------------------- ### Pagination Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example demonstrating how to create a Pagination iterator instance, both with and without deprecated parameters. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) # Create pagination iterator pagination = search.pagination(limit=3) # Or using deprecated parameters pagination = Pagination(search, start=0, end=30, num=10, limit=3) ``` -------------------------------- ### Constructor Parameters - params_dict Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md An example showing how to initialize GoogleSearch with a dictionary of parameters, including query, location, device, API key, and pagination settings. ```python from serpapi import GoogleSearch params = { "q": "coffee", "location": "Austin,Texas", "device": "mobile", "api_key": "your_api_key", "num": 10, "start": 0 } search = GoogleSearch(params) results = search.get_dict() ``` -------------------------------- ### Get Response Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example showing how to get the raw HTTP response object from the SerpApiClient. ```python response = client.get_response() # response is a requests.Response object print(response.status_code) print(response.headers) ``` -------------------------------- ### Get Results Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example demonstrating how to retrieve the response body as a string using get_results. ```python result_text = client.get_results() # Returns raw response as string ``` -------------------------------- ### Construct URL Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example demonstrating how to construct the full URL and parameters for an API request using SerpApiClient. ```python client = SerpApiClient({ "q": "coffee", "engine": "google", "api_key": "your_api_key" }) url, params = client.construct_url() # Returns: ("https://serpapi.com/search", {params...}) ``` -------------------------------- ### Get JSON Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example showing how to retrieve parsed JSON search results as a Python dictionary. ```python results = client.get_json() print(results["search_metadata"]["id"]) print(results["organic_results"][0]["title"]) ``` -------------------------------- ### Basic Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Example of how to perform a basic Home Depot search for a 'chair'. ```python from serpapi import HomeDepotSearch # Basic search search = HomeDepotSearch({ "q": "chair", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### YouTube Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a YouTube Search. ```python from serpapi import YoutubeSearch search = YoutubeSearch({ "search_query": "python tutorial", # Note: YouTube uses 'search_query' "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### YoutubeSearch Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Example of creating a YoutubeSearch instance and performing a basic search. ```python from serpapi import YoutubeSearch # Basic search (note: use search_query parameter) search = YoutubeSearch({ "search_query": "chair", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### __next__ Method Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example demonstrating the usage of the __next__ method (called automatically in a for loop) to iterate through search results pages and access data. ```python pagination = search.pagination(limit=5) for page in pagination: # __next__ called automatically print(f"Page {page['serpapi_pagination']['current']}") for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Get Raw JSON Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example for retrieving search results as a raw JSON string without parsing. ```python json_str = client.get_raw_json() # Use json.loads() to parse if needed parsed = json.loads(json_str) ``` -------------------------------- ### Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/duck_duck_go_search.md Example of how to create a new DuckDuckGoSearch instance and perform a basic search. ```python from serpapi import DuckDuckGoSearch # Basic search (note: use query parameter) search = DuckDuckGoSearch({ "query": "chair", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### __iter__ Method Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example showing how the __iter__ method is implicitly used within a for loop for pagination. ```python pagination = search.pagination(limit=3) # __iter__ is called automatically in a for loop for page in pagination: print(page) ``` -------------------------------- ### eBay Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for an eBay Search. ```python from serpapi import EbaySearch search = EbaySearch({ "_nkw": "coffee maker", # Note: eBay uses '_nkw' "_udlo": "50", # Min price "_udhi": "200", # Max price "LH_ItemCondition": "New", # Item condition "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Google Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Google Search. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "python programming", "location": "Austin,Texas", "device": "desktop", "hl": "en", # UI language "gl": "us", # Country "safe": "active", # Safe search "num": 10, # Results per page "start": 0, # Offset "tbm": "isch", # Type: isch (images), nws (news), shop (shopping) "tbs": "qdr:d", # Time filter: last 24 hours "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### WalmartSearch Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Example of creating a WalmartSearch instance and performing a basic search. ```python from serpapi import WalmartSearch # Basic search (note: use query parameter) search = WalmartSearch({ "query": "chair", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Bing Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Bing Search. ```python from serpapi import BingSearch search = BingSearch({ "q": "python", "location": "Austin,Texas", "count": 10, # Results per page "offset": 0, # Pagination offset "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Get Object Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Example of getting search results as a dynamic Python object. ```python object_result = search.get_object() ``` -------------------------------- ### Baidu Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Baidu Search. ```python from serpapi import BaiduSearch search = BaiduSearch({ "q": "python", "pn": 0, # Page number "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### BaiduSearch Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/baidu_search.md Example of creating a BaiduSearch instance and performing a basic search. ```python from serpapi import BaiduSearch # Basic search (note: use q parameter, not custom parameters) search = BaiduSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Set Global Key Once Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of setting the global API key once at application startup. ```python # Set at application startup from serpapi import GoogleSearch import os GoogleSearch.SERP_API_KEY = os.getenv("SERP_API_KEY") # Use throughout application without passing key each time search1 = GoogleSearch({"q": "coffee"}) search2 = GoogleSearch({"q": "tea"}) ``` -------------------------------- ### Configuring Pagination Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of how to configure pagination to retrieve multiple pages of results. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key", "num": 10 # Results per page }) # Get first 5 pages for page in search.pagination(limit=5): for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Yahoo Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Yahoo Search. ```python from serpapi import YahooSearch search = YahooSearch({ "p": "python", # Note: Yahoo uses 'p' not 'q' "b": 1, # Page number "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Create a generic search client for Google Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example of creating a SerpApiClient instance for Google search with specified parameters. ```python from serpapi import SerpApiClient # Create a generic search client for Google client = SerpApiClient({ "q": "coffee", "location": "Austin,Texas", "engine": "google", "api_key": "your_api_key" }) ``` -------------------------------- ### Constructor Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/google_scholar_search.md Example of creating a GoogleScholarSearch instance and performing a basic search. ```python from serpapi import GoogleScholarSearch # Basic search search = GoogleScholarSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Constructor Parameters - timeout Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Shows how to set a custom request timeout for a GoogleSearch instance in milliseconds. ```python from serpapi import GoogleSearch # Set 30-second timeout search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }, timeout=30000) results = search.get_dict() ``` -------------------------------- ### Create a client with engine parameter Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example of creating a SerpApiClient instance, specifying the engine as a separate parameter. ```python from serpapi import SerpApiClient client = SerpApiClient( {"q": "coffee", "api_key": "your_api_key"}, engine="google" ) ``` -------------------------------- ### Export API Key Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Example of setting the API key as an environment variable. ```bash export API_KEY="your secret key" ``` -------------------------------- ### Account API Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Fetches and prints the user's account information. ```python from serpapi import GoogleSearch search = GoogleSearch({}) account = search.get_account() ``` -------------------------------- ### get_account() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves account information using the Account API, including credits, subscription details, and plan ID. ```python account_info = client.get_account() print(account_info["credits_used"]) print(account_info["subscription_is_active"]) print(account_info["plan_id"]) ``` -------------------------------- ### Search for Music Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Example of searching for music on YouTube. ```python search = YoutubeSearch({ "search_query": "jazz music", "api_key": "your_api_key" }) results = search.get_dict() for video in results.get("video_results", []): print(f"{video['title']}: {video['link']}") ``` -------------------------------- ### Pagination Through Results Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Example of paginating through Home Depot search results for 'flooring'. ```python search = HomeDepotSearch({ "q": "flooring", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for product in page.get("product_results", []): print(product["title"]) ``` -------------------------------- ### Get App Details Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/apple_app_store_search.md Example of retrieving and printing detailed information for apps found in the Apple App Store search results. ```python search = AppleAppStoreSearch({ "term": "productivity", "api_key": "your_api_key" }) results = search.get_dict() for app in results.get("app_results", []): print(f"App: {app['title']}") print(f" Developer: {app.get('developer')}") print(f" Rating: {app.get('rating')} ({app.get('review_count')} reviews)") print(f" Price: {app.get('price', 'Free')}") print(f" Category: {app.get('category')}") ``` -------------------------------- ### Yandex Search Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Yandex Search. ```python from serpapi import YandexSearch search = YandexSearch({ "text": "python", # Note: Yandex uses 'text' "p": 0, # Page number "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Pagination Through Results Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yandex_search.md Example demonstrating how to paginate through Yandex search results. ```python search = YandexSearch({ "text": "coffee", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Setting API Key in Bash Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of setting the API key environment variable in Bash. ```bash export SERP_API_KEY="your_api_key" python your_script.py ``` -------------------------------- ### Search Walmart Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Walmart search for 'chair' and returns the results as a dictionary. ```python from serpapi import WalmartSearch search = WalmartSearch({"query": "chair"}) data = search.get_dict() ``` -------------------------------- ### Google Scholar Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of configuring parameters for a Google Scholar Search. ```python from serpapi import GoogleScholarSearch search = GoogleScholarSearch({ "q": "machine learning", "sort": "date", # Sort by date "as_ylo": "2020", # From year "as_yhi": "2024", # To year "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Search for Furniture Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Example of searching for 'outdoor furniture' on Home Depot. ```python search = HomeDepotSearch({ "q": "outdoor furniture", "api_key": "your_api_key" }) results = search.get_dict() for product in results.get("product_results", []): print(product["title"]) ``` -------------------------------- ### Search for Electronics Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Example of searching for electronic products like laptops on Walmart. ```python search = WalmartSearch({ "query": "laptop", "api_key": "your_api_key" }) results = search.get_dict() for product in results.get("product_results", []): print(f"{product['title']}") print(f ``` -------------------------------- ### Search Home Depot Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Home Depot search for 'chair' and returns the results as a dictionary. ```python from serpapi import HomeDepotSearch search = HomeDepotSearch({"q": "chair"}) data = search.get_dict() ``` -------------------------------- ### Location Not Supported Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Demonstrates handling the exception when trying to use get_location() with HomeDepotSearch. ```python from serpapi import HomeDepotSearch from serpapi.serp_api_client_exception import SerpApiClientException search = HomeDepotSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Austin") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by Home Depot search engine ``` -------------------------------- ### Pagination Through Results Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/naver_search.md Example of paginating through Naver search results. ```python search = NaverSearch({ "query": "의자", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for product in page.get("shopping_results", []): print(product["title"]) ``` -------------------------------- ### get_dict() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves search results as a dictionary. This is an alias for get_dictionary(). ```python results = client.get_dict() for result in results["organic_results"]: print(result["title"]) ``` -------------------------------- ### Location Not Supported Error Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md This example shows how a SerpApiClientException is raised when attempting to use the get_location() method on an unsupported search engine like BaiduSearch. ```python from serpapi import BaiduSearch from serpapi.serp_api_client_exception import SerpApiClientException search = BaiduSearch({"api_key": "your_api_key"}) try: locations = search.get_location("Beijing", limit=5) except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by Baidu search engine at this time ``` -------------------------------- ### Timeout Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Demonstrates setting a timeout for GoogleSearch requests, showing examples for interactive and batch operations. ```python from serpapi import GoogleSearch # For fast, interactive use search = GoogleSearch({ "q": "coffee", "api_key": api_key }, timeout=10000) # 10 seconds # For batch operations search = GoogleSearch({ "q": "coffee", "api_key": api_key }, timeout=60000) # 60 seconds ``` -------------------------------- ### Basic Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/ebay_search.md Example of how to perform a basic eBay search using EbaySearch, including the use of the '_nkw' parameter for the query. ```python from serpapi import EbaySearch # Basic search (note: use _nkw parameter for eBay) search = EbaySearch({ "_nkw": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Search Baidu Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Baidu search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import BaiduSearch search = BaiduSearch({"q": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### get_dictionary() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves search results as a dictionary. This is an alias for get_json(). ```python data = client.get_dictionary() ``` -------------------------------- ### Pagination Through Results Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Demonstrates how to paginate through YouTube search results. ```python search = YoutubeSearch({ "search_query": "chair", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for video in page.get("video_results", []): print(video["title"]) ``` -------------------------------- ### Setting API Key in Python Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Example of setting the API key environment variable within a Python script. ```python import os os.environ["SERP_API_KEY"] = "your_api_key" ``` -------------------------------- ### Search eBay Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs an eBay search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import EbaySearch search = EbaySearch({"_nkw": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### API Key Setup Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/INDEX.md Demonstrates how to set the API key for the GoogleSearch client, either globally or per instance. ```python GoogleSearch.SERP_API_KEY = "your_api_key" # OR search = GoogleSearch({"q": "coffee", "api_key": "your_api_key"}) ``` -------------------------------- ### Pagination Through Results Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yahoo_search.md Demonstrates how to paginate through Yahoo search results. ```python search = YahooSearch({ "p": "coffee", "api_key": "your_api_key" }) for page in search.pagination(limit=3): for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Dynamic Object Access Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Example of accessing search results using dynamic object properties. ```python search = HomeDepotSearch({ "q": "flooring", "api_key": "your_api_key" }) result = search.get_object() if result.product_results: print(result.product_results[0].title) ``` -------------------------------- ### Basic YouTube Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Demonstrates a basic YouTube search using the YoutubeSearch client. ```python from serpapi import YoutubeSearch search = YoutubeSearch({ "search_query": "python tutorial", # Note: YouTube uses 'search_query' parameter "api_key": "your_api_key" }) results = search.get_dict() if results.get("error") is None: for video in results.get("video_results", []): print(f"{video['title']} - {video.get('views')} views") ``` -------------------------------- ### Reproducing the Missing Engine Parameter Error Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md This example demonstrates how to trigger a SerpApiClientException when the 'engine' parameter is missing. ```python from serpapi import SerpApiClient from serpapi.serp_api_client_exception import SerpApiClientException try: # Missing both engine parameter and engine in params client = SerpApiClient({"q": "coffee"}) results = client.get_dict() except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: engine must be defined in params_dict or engine ``` -------------------------------- ### Search Bing Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Bing search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import BingSearch search = BingSearch({"q": "Coffee", "location": "Austin,Texas"}) data = search.get_dict() ``` -------------------------------- ### Search Youtube Example 2 Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Youtube search for 'chair' and returns the results as a dictionary. ```python from serpapi import YoutubeSearch search = YoutubeSearch({"search_query": "chair"}) data = search.get_dict() ``` -------------------------------- ### Constructor Parameters - engine Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Demonstrates using the generic SerpApiClient with the 'engine' parameter to specify the search engine, and shows the equivalent using a specialized search class like GoogleSearch. ```python from serpapi import SerpApiClient # Using generic client with engine parameter client = SerpApiClient( {"q": "coffee", "api_key": "your_api_key"}, engine="google" ) results = client.get_dict() # Equivalent to using GoogleSearch from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Search Youtube Example 1 Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Youtube search for 'chair' and returns the results as a dictionary. ```python from serpapi import YoutubeSearch search = YoutubeSearch({"q": "chair"}) data = search.get_dict() ``` -------------------------------- ### Handling Timeouts Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Provides an example of how to catch a `requests.Timeout` exception when a request exceeds the specified timeout duration. ```python import requests from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }, timeout=5000) # 5 second timeout try: results = search.get_dict() except requests.Timeout: print("Request timed out - increase timeout or check connection") ``` -------------------------------- ### Search Yahoo Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Yahoo search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import YahooSearch search = YahooSearch({"p": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### Location API Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Fetches a list of locations matching a given query, printing the first 3. ```python from serpapi import GoogleSearch search = GoogleSearch({}) location_list = search.get_location("Austin", 3) print(location_list) ``` -------------------------------- ### Pagination with Filter Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example of paginating through results and filtering them based on a specific domain. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "python", "api_key": "your_api_key" }) target_domain = "github.com" filtered_results = [] for page in search.pagination(limit=5): for result in page.get("organic_results", []): if target_domain in result.get("link", ""): filtered_results.append(result) print(f"Found {len(filtered_results)} results from {target_domain}") ``` -------------------------------- ### Search Apple App Store Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs an Apple App Store search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import AppleAppStoreSearch search = AppleAppStoreSearch({"term": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### Comprehensive Error Handling Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md A detailed example showing how to catch various exceptions including `SerpApiClientException`, `requests.HTTPError`, `requests.Timeout`, and general `requests.RequestException`. ```python from serpapi import GoogleSearch from serpapi.serp_api_client_exception import SerpApiClientException import requests search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) try: results = search.get_dict() if results.get("error"): print(f"API Error: {results['error']}") else: print(f"Success: Found {len(results.get('organic_results', []))} results") except SerpApiClientException as e: print(f"SerpApi Client Error: {e}") except requests.HTTPError as e: print(f"HTTP Error {e.response.status_code}: {e}") except requests.Timeout: print("Request timed out") except requests.RequestException as e: print(f"Request Error: {e}") except Exception as e: print(f"Unexpected error: {e}") ``` -------------------------------- ### Paginate News Results Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example demonstrating how to paginate through news search results by setting the 'tbm' parameter to 'nws' and collecting articles. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "bitcoin", "tbm": "nws", # News "api_key": "your_api_key" }) # Collect news across multiple pages articles = [] for page in search.pagination(limit=3): for article in page.get("news_results", []): articles.append(article) print(f"Total articles: {len(articles)}") ``` -------------------------------- ### get_location() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves location information using the Location API and demonstrates how to use the returned location in a subsequent search. ```python locations = client.get_location("Austin", limit=3) # Returns: # [ # { # "id": "585069bdee19ad271e9bc072", # "name": "Austin, TX", # "canonical_name": "Austin,TX,Texas,United States", # "country_code": "US", # "google_id": 200635, # "gps": [-97.7430608, 30.267153], # "reach": 5560000, # "target_type": "DMA Region" # }, # ... # ] # Use the location in a search first_location = locations[0]["canonical_name"] client.params_dict["location"] = first_location results = client.get_dict() ``` -------------------------------- ### get_object() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves search results wrapped in a dynamically created Python object for attribute-style access. ```python result = client.get_object() # Access nested data using dot notation print(result.search_metadata.id) print(result.organic_results[0].title) print(result.search_parameters.q) ``` -------------------------------- ### Pagination with Different Search Engines (Bing) Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example showing how to use the pagination feature with a different search engine, Bing, to retrieve search results. ```python from serpapi import BingSearch search = BingSearch({ "q": "python", "api_key": "your_api_key" }) # Bing pagination for page in search.pagination(limit=2): for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Search Yandex Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Yandex search for 'Coffee' and returns the results as a dictionary. ```python from serpapi import YandexSearch search = YandexSearch({"text": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### Pagination Through Results Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Demonstrates how to paginate through Walmart search results using the pagination method. ```python search = WalmartSearch({ "query": "chair", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for product in page.get("product_results", []): print(product["title"]) ``` -------------------------------- ### Basic Walmart Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Demonstrates a basic Walmart product search using the 'query' parameter. ```python from serpapi import WalmartSearch search = WalmartSearch({ "query": "furniture", # Note: Walmart uses 'query' parameter "api_key": "your_api_key" }) results = search.get_dict() if results.get("error") is None: for product in results.get("product_results", []): print(f"{product['title']} - ${product.get('price')}") ``` -------------------------------- ### Search in Russian Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yandex_search.md Example of performing a search in Russian using the 'text' parameter. ```python search = YandexSearch({ "text": "кофе", # Coffee in Russian "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Basic Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/bing_search.md Demonstrates a basic Bing search for 'coffee' and retrieves results as a dictionary. ```python from serpapi import BingSearch # Basic search search = BingSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Dynamic Object Access Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Shows how to access search results as a dynamic Python object. ```python search = WalmartSearch({ "query": "furniture", "api_key": "your_api_key" }) result = search.get_object() if result.product_results: product = result.product_results[0] print(product.title) print(product.price) ``` -------------------------------- ### get_search_archive() Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Retrieves a previously cached search result from the Search Archive API. This call is free and does not consume API quota. ```python # Store the ID from a previous search results = client.get_dict() search_id = results["search_metadata"]["id"] # Retrieve from archive later (free call) archived = client.get_search_archive(search_id) # Same results without consuming API quota ``` -------------------------------- ### Collecting Links Across Multiple Pages Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Example demonstrating how to collect all URLs from organic results across multiple pages of a search query. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "machine learning", "api_key": "your_api_key", "num": 10 # 10 results per page }) urls = [] for page in search.pagination(limit=5): # Get 5 pages (50 results) for result in page.get("organic_results", []): urls.append(result["link"]) print(f"Collected {len(urls)} URLs") ``` -------------------------------- ### Basic Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yandex_search.md Example of performing a basic search using YandexSearch. Note the use of the 'text' parameter instead of 'q'. ```python from serpapi import YandexSearch # Basic search (note: use text parameter, not q) search = YandexSearch({ "text": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Handling API Response Errors Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md This example shows how to check for API-level errors by examining the 'error' key in the returned dictionary, as opposed to exceptions. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() # Check for 'error' key in results dictionary ``` -------------------------------- ### Handling HTTP Errors Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md This example demonstrates how to catch and handle `requests.HTTPError` which can occur due to non-2xx status codes, such as an invalid API key. ```python from serpapi import GoogleSearch import requests search = GoogleSearch({ "q": "coffee", "api_key": "invalid_key" # Wrong API key }) try: results = search.get_dict() except requests.HTTPError as e: print(f"HTTP Error: {e}") print(f"Status code: {e.response.status_code}") print(f"Response: {e.response.text}") ``` -------------------------------- ### Constructor Signature Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md The signature for the __init__ constructor of HomeDepotSearch. ```python def __init__(self, params_dict) ``` -------------------------------- ### Add new parameters Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Demonstrates how to add new parameters to the search dictionary. ```python search.params_dict["device"] = "mobile" results = search.get_dict() ``` -------------------------------- ### Import HomeDepotSearch Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Import the HomeDepotSearch class from the serpapi library. ```python from serpapi import HomeDepotSearch ``` -------------------------------- ### Environment Variables for API Key Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Shows how to read the API key from environment variables for secure storage. ```python import os from serpapi import GoogleSearch # Read API key from environment api_key = os.getenv("SERP_API_KEY") if not api_key: raise ValueError("SERP_API_KEY environment variable not set") search = GoogleSearch({ "q": "coffee", "api_key": api_key }) results = search.get_dict() ``` -------------------------------- ### Setting the Global API Key Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Demonstrates how to set a global API key for GoogleSearch and BingSearch classes. This key will be used by all instances created from these classes. ```python from serpapi import GoogleSearch, BingSearch # Set global API key for GoogleSearch GoogleSearch.SERP_API_KEY = "your_private_api_key" # Now all GoogleSearch instances will use this key search1 = GoogleSearch({"q": "coffee"}) search2 = GoogleSearch({"q": "tea"}) # Both will use the global key # Set global key for other search engines BingSearch.SERP_API_KEY = "your_private_api_key" ``` -------------------------------- ### Pagination Through Results Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/duck_duck_go_search.md Example of paginating through search results using DuckDuckGoSearch. ```python search = DuckDuckGoSearch({ "query": "chair", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for result in page.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Handle Pagination Gracefully Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/pagination.md Demonstrates how to gracefully handle pagination, including collecting results and stopping when no more pages are available. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) results_collected = 0 try: for page in search.pagination(limit=10): page_results = page.get("organic_results", []) results_collected += len(page_results) for result in page_results: print(result["title"]) except StopIteration: print(f"Pagination complete. Collected {results_collected} results.") ``` -------------------------------- ### Secure API Key Storage Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Demonstrates secure API key storage using environment variables compared to hardcoding. ```python # Bad: Hardcoded API key search = GoogleSearch({"q": "coffee", "api_key": "xyz123"}) # Good: Use environment variables import os api_key = os.getenv("SERP_API_KEY") search = GoogleSearch({"q": "coffee", "api_key": api_key}) ``` -------------------------------- ### Location Not Supported Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Illustrates the exception raised when attempting to use get_location() with YoutubeSearch. ```python from serpapi import YoutubeSearch from serpapi.serp_api_client_exception import SerpApiClientException search = YoutubeSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Austin") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by youtube search engine ``` -------------------------------- ### Location Not Supported Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yandex_search.md Demonstrates the exception raised when attempting to use get_location() with YandexSearch. ```python from serpapi import YandexSearch from serpapi.serp_api_client_exception import SerpApiClientException search = YandexSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Moscow") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by Yandex search engine at this time ``` -------------------------------- ### Basic Naver Search Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/naver_search.md Example of a basic Naver search with a Korean search term. ```python from serpapi import NaverSearch search = NaverSearch({ "query": "쇼핑", # Korean search term "api_key": "your_api_key" }) results = search.get_dict() if results.get("error") is None: for product in results.get("shopping_results", []): print(f"{product['title']} - {product.get('price')}") ``` -------------------------------- ### Dynamic Object Access Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/youtube_search.md Shows how to access search results as a dynamic Python object. ```python search = YoutubeSearch({ "search_query": "python tutorial", "api_key": "your_api_key" }) result = search.get_object() if result.video_results: print(result.video_results[0].title) print(result.video_results[0].views) ``` -------------------------------- ### Run Test Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Command to run the test suite for the library. ```python make test ``` -------------------------------- ### Engine-Specific Configuration Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/configuration.md Shows how to configure different search engines (GoogleSearch, YahooSearch) with their respective parameters. ```python from serpapi import GoogleSearch, YahooSearch google = GoogleSearch({ "q": "python", "location": "Austin,Texas", # Google supports location "api_key": api_key }) yahoo = YahooSearch({ "p": "python", # Yahoo uses 'p' # Note: Yahoo doesn't support location filtering "api_key": api_key }) ``` -------------------------------- ### Pagination Through App Results Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/apple_app_store_search.md Example demonstrating how to paginate through app search results using the pagination method with a specified limit. ```python search = AppleAppStoreSearch({ "term": "ipad", "api_key": "your_api_key" }) for page in search.pagination(limit=2): for app in page.get("app_results", []): print(app["title"]) ``` -------------------------------- ### Error Handling Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/google_search.md Provides an example of how to check for and handle potential errors returned in the search results. ```python from serpapi import GoogleSearch search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() if results.get("error"): print(f"Error: {results['error']}") else: print(f"Found {len(results['organic_results'])} results") ``` -------------------------------- ### Search with Custom Parameters Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/duck_duck_go_search.md Example of performing a search with custom parameters using DuckDuckGoSearch. ```python search = DuckDuckGoSearch({ "query": "python programming", "api_key": "your_api_key" }) results = search.get_dict() for result in results.get("organic_results", []): print(result["title"]) ``` -------------------------------- ### Setting Global API Key Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Demonstrates two ways to set the SerpApi API key: globally for all instances or per instance within the params_dict. ```python # Set globally GoogleSearch.SERP_API_KEY = "your_private_key" # Or per instance in params_dict search = GoogleSearch({"q": "coffee", "api_key": "your_api_key"}) ``` -------------------------------- ### Search Google Scholar Example Source: https://github.com/serpapi/google-search-results-python/blob/master/README.md Performs a Google Scholar search for 'Coffee' and returns the results. ```python from serpapi import GoogleScholarSearch search = GoogleScholarSearch({"q": "Coffee"}) data = search.get_dict() ``` -------------------------------- ### Basic Home Depot Search Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/home_depot_search.md Demonstrates a basic Home Depot search and how to process the results. ```python from serpapi import HomeDepotSearch search = HomeDepotSearch({ "q": "chair", "api_key": "your_api_key" }) results = search.get_dict() if results.get("error") is None: for result in results.get("product_results", []): print(f"{result['title']} - ${result.get('price')}") ``` -------------------------------- ### Collect links across multiple pages Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md Example demonstrating how to collect links from multiple news search result pages using the pagination feature. ```python urls = [] client.params_dict = { "q": "coca cola", "tbm": "nws", "engine": "google", "api_key": "your_api_key", "start": 0, "num": 10 } pages = client.pagination(limit=3) # Get 3 pages for page in pages: print(f"Page {page['serpapi_pagination']['current']}") for result in page["news_results"]: urls.append(result["link"]) print(f"Total URLs collected: {len(urls)}") ``` -------------------------------- ### Search with Location Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/bing_search.md Shows how to perform a localized Bing search for 'coffee' in 'Austin,Texas'. ```python from serpapi import BingSearch # Search with location search = BingSearch({ "q": "coffee", "location": "Austin,Texas", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Dynamic Object Access Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/google_scholar_search.md Example of accessing search results as dynamic Python objects. ```python search = GoogleScholarSearch({ "q": "artificial intelligence", "api_key": "your_api_key" }) result = search.get_object() if result.organic_results: paper = result.organic_results[0] print(paper.title) print(f"Cited by: {paper.cited_by}") ``` -------------------------------- ### Import SerpApiClient Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/serp_api_client.md How to import the SerpApiClient class from the serpapi library. ```python from serpapi import SerpApiClient ``` -------------------------------- ### Location Not Supported Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/walmart_search.md Illustrates the exception raised when attempting to use get_location() with WalmartSearch. ```python from serpapi import WalmartSearch from serpapi.serp_api_client_exception import SerpApiClientException search = WalmartSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Austin") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by walmart search engine ``` -------------------------------- ### Location Not Supported Exception Handling Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/duck_duck_go_search.md Example demonstrating how to handle the SerpApiClientException when calling get_location on DuckDuckGoSearch. ```python from serpapi import DuckDuckGoSearch from serpapi.serp_api_client_exception import SerpApiClientException search = DuckDuckGoSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Austin") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by walmart search engine ``` -------------------------------- ### Basic Yahoo Search Example Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/yahoo_search.md Demonstrates how to perform a basic Yahoo search using the YahooSearch client. Note the use of the 'p' parameter for the query. ```python from serpapi import YahooSearch # Basic search (note: use p parameter for Yahoo, not q) search = YahooSearch({ "p": "coffee", "api_key": "your_api_key" }) results = search.get_dict() ``` -------------------------------- ### Sort by Date Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/google_scholar_search.md Example of sorting search results by date using the 'sort' parameter. ```python search = GoogleScholarSearch({ "q": "machine learning", "sort": "date", # Sort by most recent "api_key": "your_api_key" }) results = search.get_dict() for paper in results.get("organic_results", []): print(paper["title"]) ``` -------------------------------- ### Logging Errors Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/errors.md Demonstrates how to integrate logging with the search process to record API errors, successful fetches, and unexpected exceptions. ```python import logging from serpapi import GoogleSearch logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) try: search = GoogleSearch({ "q": "coffee", "api_key": "your_api_key" }) results = search.get_dict() if results.get("error"): logger.error(f"API Error: {results['error']}") else: logger.info(f"Successfully fetched {len(results['organic_results'])} results") except Exception as e: logger.exception(f"Unexpected error during search: {e}") ``` -------------------------------- ### Handling Location Not Supported Exception Source: https://github.com/serpapi/google-search-results-python/blob/master/_autodocs/api-reference/baidu_search.md Example demonstrating how to catch the SerpApiClientException when calling get_location on BaiduSearch. ```python from serpapi import BaiduSearch from serpapi.serp_api_client_exception import SerpApiClientException search = BaiduSearch({ "api_key": "your_api_key" }) try: locations = search.get_location("Beijing") except SerpApiClientException as e: print(f"Error: {e}") # Output: Error: location is not supported by Baidu search engine at this time ```