How to Create Question Explorer Tool from Keywords

Getting questions from google search console

import requests
from xml.etree import ElementTree

def get_questions(question_type: str, user_input: str, country_code: str) -> List[str]:
    question_results = []  # List to return

    # Build Google Search query
    search_query = f"{question_type} {user_input} "
    google_search_url = f"http://google.com/complete/search?output=toolbar&gl={country_code}&q={search_query}"

    # Call the URL and read data
    response = requests.get(google_search_url)
    result = response.text

    # Parse the XML document
    doc = ElementTree.fromstring(result)

    for element in doc.findall(".//CompleteSuggestion"):
        question = element.find(".//suggestion").attrib.get("data")
        question_results.append(question)

    return question_results

This function can be used in a Django view or any other part of your Django application. You can call it by passing the appropriate values for the question_type, user_input, and country_code arguments.

For example, you could use it in a Django view like this:

from django.shortcuts import render

def search_view(request):
    # Get the search keyword from the GET request parameters
    search_keyword = request.GET.get("keyword")

    # Get the questions using the get_questions function
    questions = get_questions("what", search_keyword, "us")

    # Render the search results template
    return render(request, "search_results.html", {"questions": questions})

This view function would retrieve the search keyword from the GET request parameters and pass it to the get_questions function along with the question type and country code. It would then render the search_results.html template, passing the list of questions as a context variable.

Writing the table for search_results.html

Here is an example of how you could create a table in the search_results.html template to display the search results:

<table>
  <thead>
    <tr>
      <th>Question</th>
    </tr>
  </thead>
  <tbody>
    {% for question in questions %}
      <tr>
        <td>{{ question }}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

This table will display a row for each question in the questions list, with the question text displayed in the first column.

You can customize the table by adding more columns or modifying the existing ones as needed. For example, you could add a second column to display the search keyword associated with each question:

<table>
  <thead>
    <tr>
      <th>Question</th>
      <th>Keyword</th>
    </tr>
  </thead>
  <tbody>
    {% for question in questions %}
      <tr>
        <td>{{ question }}</td>
        <td>{{ search_keyword }}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

You will need to pass the search_keyword variable to the template when rendering it in the Django view, as shown in the previous example.

Writing a view function that accepts the POST request from the form for ” get_questions ” function

Here is an example of a Django view function that accepts a POST request from a form and calls the get_questions function to retrieve a list of questions:

from django.shortcuts import render
from django.http import HttpResponseRedirect

def search_view(request):
    if request.method == "POST":
        # Get the form data
        question_type = request.POST.get("question_type")
        search_keyword = request.POST.get("search_keyword")
        country_code = request.POST.get("country_code")

        # Call the get_questions function
        questions = get_questions(question_type, search_keyword, country_code)

        # Render the search results template
        return render(request, "search_results.html", {"questions": questions, "search_keyword": search_keyword})

    # Render the search form template
    return render(request, "search_form.html")

This view function first checks the request method and, if it is a POST request, retrieves the form data from the request and passes it to the get_questions function. It then renders the search_results.html template, passing the list of questions and the search keyword as context variables.

If the request method is not POST, the view function renders the search_form.html template, which should contain the form for submitting the search request.

You can use the following HTML code for the search_form.html template:

<form method="post">
  {% csrf_token %}
  <label for="question_type">Question Type:</label><br>
  <input type="text" id="question_type" name="question_type"><br>
  <label for="search_keyword">Search Keyword:</label><br>
  <input type="text" id="search_keyword" name="search_keyword"><br>
  <label for="country_code">Country Code:</label><br>
  <input type="text" id="country_code" name="country_code"><br><br>
  <input type="submit" value="Submit">
</form>

This form includes fields for the question_type, search_keyword, and country_code parameters, and a submit button to send the form data to the server when clicked. The csrf_token template tag is included to protect against cross-site request forgery attacks.

Now how to connect Google Search suggestion API for the above functions and where to place the key?

To use the Google Search suggestion API, you will need to obtain an API key and enable the API.

Here is the general process for obtaining an API key and enabling the API:

  1. Go to the Google Cloud Console.
  2. Click the project drop-down and select or create the project that you want to use for the API.
  3. Click the hamburger menu and select APIs & Services > Library.
  4. Search for “Custom Search API” and click on the API in the search results.
  5. Click the Enable button to enable the API.
  6. Click the Create Credentials button.
  7. Select the “API key” option and click the Create button.

The API key will be displayed in a pop-up window. You can use this key to make requests to the API.

To use the API key in your code, you can pass it as a parameter to the get_questions function or store it in a configuration file or environment variable and retrieve it from there.

Here is an example of how you could modify the get_questions function to accept the API key as a parameter:

import requests
from xml.etree import ElementTree

def get_questions(api_key: str, question_type: str, user_input: str, country_code: str) -> List[str]:
    question_results = []  # List to return

    # Build Google Search query
    search_query = f"{question_type} {user_input} "
    google_search_url = f"https://www.googleapis.com/customsearch/v1?key={api_key}&gl={country_code}&q={search_query}"

    # Call the URL and read data
    response = requests.get(google_search_url)
    result = response.json()

    # Extract the questions from the search results
    for item in result["items"]:
        question = item.get("title")
        question_results.append(question)

    return question_results

You can then pass the API key as an argument when calling the get_questions function in your code.

Copy codeapi_key = "your_api_key"
questions = get_questions(api_key, "what", search_keyword, "us")

Alternatively, you can store the API key in a configuration file or environment variable and retrieve it from there. This can be useful if you want to keep the API key separate from your code and avoid hardcoding it in your application.

For example, you could create a config.py file with the following code:

import os

GOOGLE_SEARCH_API_KEY = os.environ["GOOGLE_SEARCH_API_KEY"]

You can then set the GOOGLE_SEARCH_API_KEY environment variable to your API key and import the GOOGLE_SEARCH_API_KEY variable from the config module in your code.

from config import GOOGLE_SEARCH_API_KEY

api_key = GOOGLE_SEARCH_API_KEY
questions = get_questions(api_key, "what", search_keyword

Related Articles

Amazon FBA How To Become a Successful Seller in 2023

This article from DOC EDUCATES is a free guide explaining how to sell on amazon for beginners, mostly explaining FBA (Fulfilled By Amazon) Business model. In this guide, we will cover all the steps needed to sell on Amazon FBA (Fulfilled By Amazon). We will show you how to set up your account, take pictures of your products, list them for sale and manage orders.

Amazon FBA How To Become a Successful Seller in 2023

This article from DOC EDUCATES is a free guide explaining how to sell on amazon for beginners, mostly explaining FBA (Fulfilled By Amazon) Business model. In this guide, we will cover all the steps needed to sell on Amazon FBA (Fulfilled By Amazon). We will show you how to set up your account, take pictures of your products, list them for sale and manage orders.

Responses

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