How to Get Google Search Results Using Python

Google search results can be invaluable for gathering information, conducting research, or automating tasks. In this tutorial, we’ll explore a method to retrieve Google search results using Python: a straightforward approach using the google-search-results package from SerpApi.

Get Search Results Using the google-search-results Package from SerpApi

Step 1: Install the necessary package

First, you’ll need to install the google-search-results package, which provides an easy-to-use API for accessing Google search results.

pip install google-search-results

Step 2: Get an API Key from SerpApi

To use this package, you need to sign up for SerpApi and get an API key. You can get a free API key with limited usage or opt for a paid plan if you need more requests.

Step 3: Write the Python Code

  • Import the GoogleSearch class: This class from the serpapi package allows you to interact with the SerpApi service.
    from serpapi import GoogleSearch
    
  • Define the get_google_search_results function: This function takes a search query and an API key as inputs and returns the search results as a dictionary.
    def get_google_search_results(query, api_key):
  • Set up the search parameters: These parameters include the search engine (google), the query (q), and your API key (api_key).
    params = {
        "engine": "google",
        "q": query,
        "api_key": api_key
    }
    
  • Create a GoogleSearch object: This object is used to perform the search with the specified parameters.
    search = GoogleSearch(params)
  • Execute the search and retrieve the results: The get_dict method of the GoogleSearch object returns the search results as a dictionary.
    results = search.get_dict()
    return results
    
  • Define the API key and your query: Here we define our API key from SerpApi and add our query that we want to search.
    # Replace 'YOUR_API_KEY' with your actual SerpApi key
    api_key = "YOUR_API_KEY"
    query = "Python programming"
    results = get_google_search_results(query, api_key)
    
  • Print the titles and links of the organic search results: Loop through the search results and print the title and link of each result.
    for result in results["organic_results"]:
        print(result["title"], "-", result["link"])
    

Here’s the output of our program,

Python Programming Language - Python.org - https://www.python.org/
Python (programming language) - Wikipedia - https://en.wikipedia.org/wiki/Python_(programming_language)
Python | Codecademy - https://www.codecademy.com/learn/learn-python-3
Learn Python Programming - Python.org - https://www.python.org/about/gettingstarted/
Python Tutorial - W3Schools - https://www.w3schools.com/python/

Please find the complete code below,

from serpapi import GoogleSearch

def get_google_search_results(query, api_key):
    params = {
        "engine": "google",
        "q": query,
        "api_key": api_key
    }
    
    search = GoogleSearch(params)
    results = search.get_dict()
    
    return results

# Replace 'YOUR_API_KEY' with your actual SerpApi key
api_key = "YOUR_API_KEY"
query = "Python programming"
results = get_google_search_results(query, api_key)

for result in results["organic_results"]:
    print(result["title"], "-", result["link"])

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top