Python SEO Keyword Research Tool: Google Autocomplete, People Also Ask and Related Searches
Tags: webscraping, python, seo, demo
Published: 2023-05-15 14:00:35

- <a href="#intro">Intro</a>
- <a href="#demo">Demonstration</a>
- <a href="#installation">Installation</a>
- <a href="#cli_usage">CLI Usage</a>
- <a href="#code_usage">In-code Usage</a>
- <a href="#links">Links</a>


<h2 id='intro'>Intro</h2>

Here's a simple user guide to help you get started with using `seo-keyword-research-tool`.

[SEO Keyword Research Tool](https://github.com/chukhraiartur/seo-keyword-research-tool) is a Python package designed to generate keywords for SEO purposes. The package is [available on PyPi](https://pypi.org/project/seo-keyword-research-tool/) and can be installed and used in your Python project or as CLI. It is used to generate keyword ideas using [Google Autocomplete](https://serpapi.com/google-autocomplete-api), [Related Searches](https://serpapi.com/related-searches) and [People also ask](https://serpapi.com/related-questions) results and save them to TXT, CSV, JSON file. 

This tool uses [SerpApi](https://serpapi.com/) as a tool to parse data from Google search results. Other search engines coming soon.

![seo-keyword-research](https://user-images.githubusercontent.com/81998012/232782247-cc2b44d3-f5fa-4cf5-b02c-8bb9f1fcaf60.png)


<h2 id='demo'>Demonstration</h2>

![image](https://user-images.githubusercontent.com/78694043/234543434-0482d07b-3db5-430e-b00a-91647bf2d9c8.gif)


<h2 id='installation'>Installation</h2>

```bash
$ pip install seo-keyword-research-tool
```

📌Note: If you want to use this tool for personal or commercial purposes, you must register on [SerpApi](https://serpapi.com/) and get your own API key (free plan available). The default API key used is for informational purposes and only works in the CLI.

Make sure you have [Python installed](https://www.python.org/downloads/) on your computer.

Before use, you need to define [your API key from SerpApi](https://serpapi.com/manage-api-key). There are several ways to do this:

- Add an environment variable `API_KEY` with your key. `$ export API_KEY=<API_KEY>` (for Linux based terminal)
- Add your key directly in code: `SeoKeywordResearch.api_key = "your_api_key"` (for in-code usage only)


<h2 id='cli_usage'>CLI Usage</h2>

You can see available usage options by typing in the terminal:

```bash
$ seo -h
```

<details>
<summary>Available arguments</summary>

```lang-none
SerpApi SEO Keyword Research Tool [-h] -q  [-e  [...]] [-dl] [-st] [-ak] [-gd] [-gl] [-hl]

Extract keywrods from: Google Autocomplete, People Also Ask, and People Also Search and saves data to CSV/JSON/TXT.

optional arguments:
  -h, --help            show this help message and exit
  -q , --query          Search query (required).
  -e  [ ...], --engines  [ ...]
                        Choices of engines to extract: Autocomplete (ac), Related Searches (rs), People Also Ask (rq). You can select multiple engines. All engines are selected by default.
  -dl , --depth-limit   Depth limit for People Also Ask. Default is 0, first 2-4 results.
  -st , --save-to       Saves the results in the current directory in the selected format (CSV, JSON, TXT). Default CSV.
  -ak , --api-key       Your SerpApi API key: https://serpapi.com/manage-api-key. Default is a test API key to test CLI.
  -gd , --domain        Google domain: https://serpapi.com/google-domains. Default google.com.
  -gl , --country       Country of the search: https://serpapi.com/google-countries. Default us.
  -hl , --lang          Language of the search: https://serpapi.com/google-languages. Default en.

Found a bug? Open issue: https://github.com/chukhraiartur/seo-keyword-research-tool/issues
```

</details>

📌Note: You can use any of the available arguments in any combination. You can see the full description of each of them below.

___

To get results for a specific query, you need to define the `--query` argument (or short: `-q`):

```bash
$ seo -q "python how to"
```

📌Note: This is a required argument.

<details>
<summary>Output</summary>

```json
{
  "auto_complete": [
    "python how to add to a list",
    "python how to round to 2 decimals",
    "python how to install pip",
    "python how to comment out a block",
    "python how to sort a list",
    "python how to call a function",
    "python how to reverse a string",
    "python how to write to a file",
    "python how to add to a dictionary",
    "python how to convert string to int",
    "python how to remove item from list",
    "python how to split a string",
    "python how to open a file",
    "python how to reverse a list",
    "python how to concatenate strings"
  ],
  "related_searches": [
    "python for beginners",
    "python course",
    "python code examples",
    "python for beginners pdf",
    "learn python",
    "python w3schools",
    "python download",
    "python programming"
  ],
  "related_questions": [
    "How do I start programming in Python?",
    "How do I teach myself Python?",
    "Is Python easy to learn?",
    "Can I self teach myself Python?"
  ]
}
Saving data in CSV format...
Data successfully saved to python_how_to.csv file
```

</details>

___

To get results from all supported engines, you don't need to pass an engine argument (as used in the example above). If you want to get results from one or two selected engines you need to define the `--engine` argument (or short: `-e`) divided by a space, e.g. `ac rs` (Autocomplete and Related Searches):

```bash
$ seo -q "lofi" -e ac
```

|Value|Engine|
|-|-|
|`ac`| Autocomplete|
|`rs`| Related Searches (People Also Search)|
|`rq`| Related Questions (People Also Ask)|

<details>
<summary>Output</summary>

```json
{
  "auto_complete": [
    "lofi girl",
    "lofi music",
    "lofi hip hop",
    "lofi boy",
    "lofi beats",
    "lofi jersey city",
    "lofi study",
    "lofi meaning",
    "lofi girl 34",
    "lofi youtube",
    "lofibra",
    "lofi study music",
    "lofi girl youtube",
    "lofi girl lore",
    "lofi girl synthwave boy"
  ]
}
Saving data in CSV format...
Data successfully saved to lofi.csv file
```

</details>

___

You can change the default google domain `google.com` to any of [supported domains list](https://serpapi.com/google-domains) by defining the `--domain` (or short: `-gd`) argument:

```bash
$ seo -q "big ben" -e rs rq -gd "google.co.uk"
```

<details>
<summary>Output</summary>

```json
{
  "related_searches": [
    "big ben london",
    "big ben country",
    "big ben clock",
    "big ben facts",
    "how old is big ben",
    "why is big ben called big ben",
    "why is big ben famous",
    "big ben tickets"
  ],
  "related_questions": [
    "Is Big Ben still under construction 2023?",
    "Why is Elizabeth Tower called Big Ben?",
    "How much does it cost to go inside Big Ben?",
    "Can you go inside Big Ben?"
  ]
}
Saving data in CSV format...
Data successfully saved to big_ben.csv file
```

</details>

___

Also, you can change the default country `us` (United States) of the search using the `--country` (or short: `-gl`) argument and set it to any of [supported country codes list](https://serpapi.com/google-countries):

```bash
$ seo -q "hotel in" -e rs -gl de
```

<details>
<summary>Output</summary>

```json
{
  "related_searches": [
    "cheap hotel in germany",
    "cheap hotel in cologne, germany",
    "hotel in baesweiler restaurant",
    "hotel in baesweiler telefonnummer",
    "germany hotel booking website",
    "hotel baesweiler speisekarte",
    "germany hotel address",
    "hotel baesweiler öffnungszeiten"
  ]
}
Saving data in CSV format...
Data successfully saved to hotel_in.csv file
```

</details>

___

If you want to change the default language `en` (English) of the search, you need to use the `--language` (or short: `-hl`) argument and set it to any of [supported language codes list](https://serpapi.com/google-languages):

```bash
$ seo -q "anime" -e ac -hl "ja" 
```

<details>
<summary>Output</summary>

```json
{
  "auto_complete": [
    "アニメ",
    "アニメイト",
    "アニメーション",
    "アニメ無料",
    "アニメ映画",
    "アニメ 英語",
    "アニメダンス",
    "アニメイト 池袋",
    "アニメ 2023",
    "アニメ人気",
    "アニメ9",
    "アニメ無料サイト",
    "anime expo",
    "anime girl",
    "anime store japan"
  ]
}
Saving data in CSV format...
Data successfully saved to anime.csv file
```

</details>

___

The default file type to save search results is `CSV`. You can change it to `JSON` or `TXT` using the `--save-to` (or short: `-st`) argument:

```bash
$ seo -q "coffee" -st json
```

<details>
<summary>Output</summary>

```json
{
  "auto_complete": [
    "coffee near me",
    "coffee belvidere il",
    "coffee rockford",
    "coffee crush",
    "coffee nearby",
    "coffee table",
    "coffee crush menu",
    "coffee maker",
    "coffee shop",
    "coffee cake recipe",
    "coffee shops rockford il",
    "coffee grinder",
    "coffee shops rockford",
    "coffee meets bagel",
    "coffee creamer"
  ],
  "related_searches": [
    "coffee near me",
    "coffee types",
    "coffee beans",
    "coffee benefits",
    "coffee starbucks",
    "coffee origin",
    "coffee plant",
    "coffee tree"
  ],
  "related_questions": [
    "Is coffee good for health?",
    "How coffee affects the body?",
    "What are the 4 types of coffee?",
    "What are 10 benefits of coffee?"
  ]
}
Saving data in JSON format...
Data successfully saved to coffee.json file
```

</details>

___

When you open People Also Ask result, it loads more results.

- Default:

![image](https://user-images.githubusercontent.com/81998012/232865316-3fbbb023-f963-4c79-aea8-ca34b7aacdae.png)

- One of the results is open:

![image](https://user-images.githubusercontent.com/81998012/232867672-55188892-ba08-412c-8e84-c6b01b6abc70.png)

So if you open all initial results that was mean you see next-depth results.

The `--depth-limit` (or short: `-dl`) argument for People Also Ask can be set from `0` to `4`. For each depth limit value, the number of results returned grows exponentially. Below is a table showing how the depth limit argument is affected:

| Depth limit | Number of results | Explanation |
|-------------|-------------------|-------------|
| 0 | 4 | Standard results |
| 1 | 12 | 4*2 = 8 + 4 = 12 |
| 2 | 36 | 8*3 = 24 + 12 = 36 |
| 3 | 108 | 24*3 = 72 + 36 = 108 |
| 4 | 324 | 72*3 = 216 + 108 = 324 |

📌Note: This is how the logic works for the `google.com` domain, on other domains the results may differ.

The example for depth `2` means that we open all standard results, then after the new results are loaded, we open them and wait until the new results are loaded:

```bash
$ seo -q "minecraft" -e rq -st txt -dl 2
```

<details>
<summary>Output</summary>

```json
{
  "related_questions": [
    "Is Minecraft OK for a 7 year old?",
    "Is Minecraft appropriate for a 5 year old?",
    "Can I play Minecraft for free?",
    "Is Roblox better than Minecraft?",
    "Is Minecraft good for kids?",
    "Is Minecraft appropriate for 4 year olds?",
    "Is Minecraft OK for 18 year olds?",
    "Is 15 too old to play Minecraft?",
    "Why is my son obsessed with Minecraft?",
    "Is Minecraft OK for 2 year olds?",
    "What is Roblox age limit?",
    "Do 30 year olds play Minecraft?",
    "What is Roblox age limit?",
    "Is Minecraft OK for 2 year olds?",
    "What is Roblox age limit?",
    "Is 15 too old to play Minecraft?",
    "Is Roblox ok for a 40 year old?",
    "Are there inappropriate games on Roblox?",
    "Why is Fortnite not for kids?",
    "Do 30 year olds play Minecraft?",
    "Is Minecraft OK for 2 year olds?",
    "Is Minecraft OK for 18 year olds?",
    "How long is Minecraft free?",
    "How to get Minecraft for free legally?",
    "How long is 100 Minecraft day?",
    "Does Minecraft cost per month?",
    "How long does 1 days in Minecraft last?",
    "What is the free version of Minecraft called?",
    "How to download real Minecraft without money?",
    "How much does Minecraft cost?",
    "Which is bigger Minecraft or Roblox?",
    "Who is the best Minecraft player alive?",
    "How do I get Robux for free?",
    "What parents need to know about Roblox?",
    "What is better fortnite or Roblox?",
    "Who is PvP God in Minecraft?",
    "Who is the clutch god in Minecraft?",
    "Who is the Minecraft King?",
    "Is Minecraft good for ADHD?",
    "What is the appropriate age to play Minecraft?",
    "Why do autistic kids like Minecraft so much?",
    "Why is my son obsessed with Minecraft?",
    "Why is my kid addicted to Minecraft?",
    "Is Minecraft good for the brain?",
    "Is 16 too old to play Minecraft?",
    "Is Minecraft addictive game?",
    "What is Roblox age limit?",
    "What age is appropriate for Roblox?",
    "Is Roblox ok for a 40 year old?",
    "Are there inappropriate games on Roblox?",
    "Why is Fortnite not for kids?",
    "How do I make Roblox safe for my child?",
    "Should I let my child chat on Roblox?",
    "Is Roblox ok for a 40 year old?"
  ]
}
Saving data in TXT format...
Data successfully saved to minecraft.txt file
```

</details>


<h2 id='code_usage'>In-code Usage</h2>

You can also use this tool in code. In fact, the arguments that you enter in the CLI are passed to the `SeoKeywordResearch` class.

Import the `SeoKeywordResearch` class to your file:

```python
from SeoKeywordResearch import SeoKeywordResearch
```

After that, you need to create an object of this class and pass all the necessary parameters:

```python
keyword_research = SeoKeywordResearch(
    query='starbucks coffee',
    api_key='<your_serpapi_api_key>',
    lang='en',
    country='us',
    domain='google.com'
)
```

To extract data from a particular engine, you need to use the appropriate methods. In the example below, I show how you can create a dictionary and add the required data by keys:

```python
data = {
    'auto_complete': keyword_research.get_auto_complete(),
    'related_searches': keyword_research.get_related_searches(),
    'related_questions': keyword_research.get_related_questions(depth_limit=1)
}
```

📌Note: The `get_related_questions()` method has the `depth_limit` parameter that allows you to load more results from People Also Ask.

You can use a convenient format to save data using the following methods:

```python
keyword_research.save_to_csv(data)
keyword_research.save_to_json(data)
keyword_research.save_to_txt(data)
```

If you need to print data in JSON format, then use the following method:

```python
keyword_research.print_data(data)
```

<details>
<summary>Output</summary>

```json
{
  "auto_complete": [
    "starbucks coffee menu",
    "starbucks coffee cups",
    "starbucks coffee traveler",
    "starbucks coffee gear",
    "starbucks coffee drinks",
    "starbucks coffee pods",
    "starbucks coffee sizes",
    "starbucks coffee creamer",
    "starbucks coffee cake",
    "starbucks coffee traveler price",
    "starbucks coffee bags",
    "starbucks coffee beans",
    "starbucks coffee near me",
    "starbucks coffee mugs",
    "starbucks coffee press"
  ],
  "related_searches": [
    "starbucks coffee menu",
    "starbucks coffee price",
    "starbucks near me",
    "starbucks coffee beans",
    "starbucks coffee cups",
    "starbucks coffee bag size",
    "starbucks coffee on sale this week",
    "amazon starbucks coffee instant"
  ],
  "related_questions": [
    "Which one is the best coffee in Starbucks?",
    "Which coffee is Starbucks famous for?",
    "What coffee is sold most at Starbucks?",
    "What is the Starbucks drink for St Patrick's Day?",
    "Which Starbucks coffee is best for beginners?",
    "How do I choose a Starbucks coffee?",
    "Why is Starbucks coffee so popular?",
    "Who is known for the best coffee?",
    "What is the most ordered drink in the world?",
    "What is the most expensive drink at Starbucks?",
    "What is the name of the green coffee at Starbucks?",
    "Does Starbucks have a healthy green drink?"
  ]
}
```

</details>


<h2 id='links'>Links</h2>

- [SerpApi](https://serpapi.com)
- [Google Autocomplete API](https://serpapi.com/google-autocomplete-api)
- [Google Related Searches API](https://serpapi.com/related-searches)
- [People Also Ask API](https://serpapi.com/related-questions)
- [App on PyPi](https://pypi.org/project/seo-keyword-research-tool/)
- [Project repository](https://github.com/chukhraiartur/seo-keyword-research-tool)

Feel free to open bug issue, something isn't working, what feature to add, or anything else related to Google autocomplete, related searches or people also ask.

If you want other functionality added to this demo project or if you want to see some other projects made with SerpApi, [write me a message](mailto:chukhraiartur@gmail.com).

___

<p style="text-align: center;">Join us on <a href="https://twitter.com/serp_api">Twitter</a> | <a href="https://www.youtube.com/channel/UCUgIHlYBOD3yA3yDIRhg_mg">YouTube</a></p>

<p style="text-align: center;">Add a  <a href="https://github.com/serpapi/public-roadmap/issues">Feature Request</a>💫 or a <a href="https://github.com/serpapi/public-roadmap/issues">Bug</a>🐞</p>