Coding Python SEO tool for scraping People Also Ask Google SERP section

How To Get Googles People Also Ask Info With Python SEO Tool

In this post we’ll to make a SEO tool with Python for scraping information from the People also ask section. In case you’re not familiar with it, this is a section that appears on Googles search results page.

Furthermore, you can find many related questions and their answers there, to what you searched for on Google.

More importantly, if you’re researching for SEO purposes, you can see what others were searching for. Therefore, you can identify their intent why they were searching for the related term and tailor your content to better suit their needs.

Coding People Also Ask (PAA) SEO Tool

We’re going to make a Python script, which we’ll be able to use straight from the command prompt. Furthermore, it’s only going to take one argument, which will be the search term.

Then, it will scrape the PAA section and save the questions and their simple answers into a .csv file in the same directory as the script.

So, like with any other Python project, first thing we need to do is import all the necessary modules. Among these are the argument parser module and scraping module, specifically designed for fetching PAA information.

You can install the scraping module by using the following command.

pip install people_also_ask
import os
import argparse
import people_also_ask
import pandas as pd

Next, we’ll need a method that will fetch the related questions and their answers, using the people_also_ask module.

This method will also return the data in a form of a dictionary, setting question as a key and the answer as its value.

def get_qnas(prompt):
    questions = people_also_ask.get_related_questions(prompt, 5)
    qnas = {}
    for q in questions:
        qnas[q] = people_also_ask.get_simple_answer(q)
    
    return qnas

Alright, now we can start putting everything in the main thread. First of all, we need to define the argument parser, which will be responsible for accepting arguments within command prompt.

parser = argparse.ArgumentParser(
    description='Fetches the questions and answers from People Also Ask section.'
)

parser.add_argument(
    '-p',
    '--prompt',
    required=True,
    help='Your search term'
)

args = parser.parse_args()

And lastly, we just need to get that PAA information and save it in a .csv file, using the pandas module.

qnas = get_qnas(args.prompt)
    
    df = pd.DataFrame.from_dict(qnas, orient='index', columns=['Answers'])
    df.index.name = 'Questions'

    print(df.head())

    df.to_csv(os.path.join(ROOT, f'{args.prompt} - PAA.csv'))

There you go! The script is finally ready to be used.

Entire code of the project

I’m also including a link to the GitHub repository, where you can check this project out.

import os
import argparse
import people_also_ask
import pandas as pd

ROOT = os.path.dirname(__file__)

def get_qnas(prompt):
    questions = people_also_ask.get_related_questions(prompt, 5)
    qnas = {}
    for q in questions:
        qnas[q] = people_also_ask.get_simple_answer(q)
    
    return qnas

if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        description='Fetches the questions and answers from People Also Ask section.'
    )

    parser.add_argument(
        '-p',
        '--prompt',
        required=True,
        help='Your search term'
    )

    args = parser.parse_args()

    qnas = get_qnas(args.prompt)
    
    df = pd.DataFrame.from_dict(qnas, orient='index', columns=['Answers'])
    df.index.name = 'Questions'

    print(df.head())

    df.to_csv(os.path.join(ROOT, f'{args.prompt} - PAA.csv'))

Conclusion

To conclude, we made a simple Python SEO tool for retrieving questions and answers from People Also Ask section on the Google SERP page. I learned a lot while working on this project and I hope it will prove helpful to you as well.

Share this article:

Related posts

Discussion(0)