Search engine optimization (SEO) is crucial for any website that wishes to rank high in search engines and attract more organic traffic. One crucial aspect of SEO is keywords research, which refers to the process of identifying the most relevant terms that users are searching for. In this article, we will explore a Python code that utilizes the power of the Google Autocomplete feature to generate relevant keyword suggestions and discuss its potential applications in SEO and customization options.

Google Autocomplete Overview

Google Autocomplete is a feature that provides real-time suggestions to users as they type their query in the search bar. These suggestions are generated based on various factors, such as popular searches and user search history. By using these suggestions, you can identify potential keywords to target and create content that is more aligned with users' needs and expectations.

Download the Google Autocomplete Python script here

    Understanding the Python Code

    Importing libraries

    The code starts by importing the necessary libraries:

    • requests: for making HTTP requests to fetch data
    • urllib: for URL manipulation and encoding
    • json: for handling JSON data structures
    • operator: for sorting purposes
    • requests_html: for HTML parsing


    1. get_source(url): This function is responsible for making the HTTP request to the provided URL and returning the response. In the case of an exception, it prints the error.

    2. get_results(query): The function takes a search query, encodes it to ensure that it follows the appropriate URL format, and uses get_source() to fetch the suggestions from Google's autocomplete service.

    3. format_results(results): This function processes the raw results from Google and returns a cleaned list of dictionaries containing the term and its relevance score (based on Google's suggestions).

    4. get_suggestions(query): It works in tandem with get_results() and format_results() to fetch, process, and sort the suggestions based on their relevance.

    5. get_expanded_term_suffixes() and get_expanded_term_prefixes(): These two functions return lists of suffixes and prefixes, respectively, that will be combined with the search query to create 'expanded' search terms. The expanded terms include different combinations and variations of the original query for more comprehensive keyword research.

    6. get_expanded_terms(query): Combines the original query with the lists of prefixes and suffixes from the previous two functions, appending the original query in between them, producing a more extensive list of search terms.

    7. get_expanded_suggestions(query): Invokes the get_expanded_terms() function and retrieves the suggestions for each expanded term. It then adds these suggestions to an aggregated list and sorts them by relevance.

    8. google_autocomplete(query, include_expanded=True): The main function that generates keyword suggestions for a given query. By default, it includes the expanded terms, but this behavior can be controlled by the include_expanded parameter.

    Example Usage

    search_term = "football betting"
    suggestions = google_autocomplete(search_term, include_expanded=True)
    for suggestion in suggestions:
        print(suggestion['term'], suggestion['relevance'])

    The output will display a list of keyword suggestions for the term "football betting" along with their relevance scores.

    Applications and Customizations

    Below are some potential applications and customizations for the Python code.

    SEO Keyword Research

    The primary application for this code is keyword research in the context of SEO. By generating a list of suggestions based on the Google Autocomplete feature, you can uncover relevant keywords that users are actively searching for. These suggestions can then be used to create targeted content, optimize existing web pages, or even generate new topic ideas for your website or blog.


    The code provided can be easily customized to suit different users or applications. For example, you can modify the following elements:

    1. Expanded terms: The lists of term prefixes and suffixes can be adjusted to focus on specific domains, industries, or topics.

    2. Language and location: By adjusting the parameters of the API call in the get_results() function, you can specify different languages and locations for more tailored results.

    3. Additional search engines or APIs: The get_source() function can also be used to retrieve suggestions from other search engines or APIs, combining the results to create more comprehensive keyword lists.

    4. Integration with other SEO tools: The code can be easily integrated with other SEO tools and libraries in Python to further enhance its functionality, such as computing keyword difficulty, search volume, or analyzing competition.


      The Python code provided in this article demonstrates an efficient way to utilize Google Autocomplete to generate relevant keyword suggestions for SEO and content creation purposes. With minor customizations, it can be further tailored to match specific requirements and make your SEO process more productive and data-driven.