Sub topic 12
Collecting suggestions with a short script
This chapter assumes you can read basic Python, have the requests library installed, and already know how to expand a seed by hand with modifiers, question words, and letters. A script does the same expansion faster and writes the results to a list you can sort in a spreadsheet.
What the script does and does not do
Autocomplete has no official public API. The script below calls an unofficial, undocumented endpoint that Google may change, limit, or remove without notice. Treat it as a convenience for small, occasional jobs, and check Google's terms before relying on it. The next chapter covers limits and responsible collection in more detail.
The script only collects suggestions. It does not tell you search volume, intent, or whether a suggestion is worth a page. Those steps come later in the workflow.
Fetching suggestions for one query
The function below sends one query and returns the list of suggestions.
import requests
SUGGEST_URL = "https://suggestqueries.google.com/complete/search"
def suggest(query, language="en"):
response = requests.get(
SUGGEST_URL,
params={"client": "firefox", "q": query, "hl": language},
timeout=10,
)
response.raise_for_status()
# The response is JSON shaped like [query, [suggestion, ...]].
return response.json()[1]
print(suggest("how to brew coffee"))The client=firefox parameter makes the endpoint return plain JSON. The hl parameter sets the interface language. The timeout stops the script from hanging, and raise_for_status() turns an HTTP error into an exception instead of silently returning bad data.
Expanding with alphabet letters
Alphabet expansion appends each letter to the seed, so "cold brew" becomes "cold brew a", "cold brew b", and so on. A set removes duplicates, and a one-second pause keeps the request rate low.
import string
import time
def expand_with_letters(seed, fetch):
ideas = set()
for letter in string.ascii_lowercase:
for suggestion in fetch(f"{seed} {letter}"):
ideas.add(suggestion.lower().strip())
time.sleep(1) # pause between requests
ideas.discard(seed.lower())
return sorted(ideas)
# Example: expand_with_letters("cold brew", suggest)Passing fetch as an argument lets you swap in a different collection function later, or a fake one for testing, without editing the loop.
Building question and modifier queries
You can also generate the question-style and modifier-style queries from the manual chapter. Type them into Google by hand, or pass each one to suggest.
QUESTION_WORDS = ["what", "why", "how", "when", "can", "is"]
MODIFIERS = ["for", "vs", "without", "near me"]
def build_queries(seed):
queries = [f"{word} {seed}" for word in QUESTION_WORDS]
queries += [f"{seed} {modifier}" for modifier in MODIFIERS]
return queries
print(build_queries("cold brew"))Common errors
- Empty or unexpected results. The endpoint may have changed its format or be limiting you. Print the raw
response.textbefore assuming your code is wrong. - HTTP 429 or blocked requests. You are sending too many requests. Stop, wait, and raise the pause between calls.
- Mixed-up results between runs. Location, language, and personalization affect what comes back, so record the
hlvalue and your location with each run. - Duplicates that look different. Differences in case and spacing create near-copies. Lowercasing and stripping, as the script does, handles most of them.
Saving the results
Once you have a sorted list, write one idea per line to a CSV file and open it in your spreadsheet. Add columns for intent, volume, and cluster as you work through the next steps.
Exercise
Why does expand_with_letters take a fetch function as an argument instead of calling suggest directly, and why does it call time.sleep(1) inside the loop?
Show answer
Taking fetch as an argument lets you replace the collection function, for example with a fake one for testing or a version with a different language, without changing the loop. The time.sleep(1) call spaces out the 26 requests so the script keeps a low request rate and is less likely to be limited or blocked.
Exercise
You run the script twice for the same seed and get slightly different lists. Name two likely causes and one thing you should record to make runs comparable.
Show answer
Likely causes are personalization or location differences and changes in the suggestions Google returns over time. Record the language (hl) value and your location, plus the date of the run, with each collection.