This post will simply show how to extract query by page into a Pandas dataframe.
Install the searchconsole package
You will also and install the following library from your command prompt or Terminal.
$ pip install searchconsole
Download API Credentials
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.
- Go to Google’s developers console, and sign-in;
- Go to “Dashboard” and click “Enable APIs and Services” ;
- Search for “Google Search Console API” and enable the API;
- Go to the “credential” tab, click on “create credential” and select “OAuth Client ID”;
- Click on “configure the consent screen ” and give a name to your product;
- Choose “Other” as the application type and click create;
- Download the JSON file.
To authenticate the API, you will need to:
- Add the folder location where you saved your credentials’ JSON file.
- Run the authentiication process
- 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.
- Define the property to extract data from
- Define the number of months to extract
- Define the dimensions that you need. In this case
- Use the get Method
site = 'https://www.example.com/' webproperty = account[site] report = webproperty.query.range('today', days=-7).dimension('query').get()
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()
This is it. A very simple introduction to one of the simplest ways to query the search console API with Python.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. In a quest to programmatic SEO for large organizations through the use of Python, R and machine learning.