Extract Query by Page Using the Searchconsole API wrapper (Python)

In this tutorial, we will learn the query Google Search Console API using Josh Carty’s Python Wrapper.

The wrapper is a very convenient way to quickly access Google Search Console data through API using Python.

This post will simply show how to extract query by page into a Pandas dataframe.


Subscribe to my Newsletter


Getting Started

For this tutorial, you will need to install Python and have a Google Search Console (GSC) account, and access level that allows you to use the API.

Install the searchconsole package

You will also and install the following library from your command prompt or Terminal.

$ pip install searchconsole

Download API Credentials

You will need an API key to be able to connect to the Google Search Console API.

Follow this guide to help you get the detailed steps to get your Google Search Console API key.

Otherwise, here are the simplified steps to get your Google Search Console API keys.

  1. Go to Google’s developers console, and sign-in;
  2. Go to “Dashboard” and click “Enable APIs and Services” ;
  3. Search for “Google Search Console API” and enable the API;
  4. Go to the “credential” tab, click on “create credential” and select “OAuth Client ID”;
  5. Click on “configure the consent screen ” and give a name to your product;
  6. Choose “Other” as the application type and click create;
  7. Download the JSON file.

Authenticate

To authenticate the API, you will need to:

  1. Add the folder location where you saved your credentials’ JSON file.
  2. Run the authentiication process
  3. Store the authenticated token into a variable.
import searchconsole
import pandas as pd
import os 

# add folder where you downloaded your credentials
creds = '/path/to/client_secrets.json'

def authenticate(config='client_secrets.json', token='credentials.json'):
    """Authenticate GSC"""
    if os.path.isfile(token):
        account = searchconsole.authenticate(client_config=config,
                                            credentials=token)
    else:
        account = searchconsole.authenticate(client_config=config,
                                        serialize=token)
    return account

account = authenticate(config=creds)

Execute the query to the API

To extract data by page and query in Search Console is very simple with the API wrapper.

  1. Define the property to extract data from
  2. Define the number of months to extract
  3. Define the dimensions that you need. In this case page and query
  4. Use the get Method

By Day

site = 'https://www.example.com/'

webproperty = account[site]
report = webproperty.query.range('today', days=-7).dimension('query').get()

By Month

site = 'https://www.example.com/'
months = -5

webproperty = account[site]
report = webproperty.query\
        .range('today', months=months)\
        .dimension('page', 'query')\
        .get()

Export the query to a Pandas DataFrame

Now, we will convert the resulting report into a Pandas dataframe to make it more usable.

df = report.to_dataframe()
df.head()

Special Thanks

Special thanks to Josh Carty for creating that fantastic library and to some of the contributors like Antoine Eripret for helping to make that library more accessible.

Conclusion

This is it. A very simple introduction to one of the simplest ways to query the search console API with Python.

5/5 - (2 votes)