How to Use Google Search Console API with Python
Updated on: by Heysho
This guide will walk you through using the Google Search Console API with Python to analyze your search performance data. We'll cover the complete process in these simple steps:
- Creating a project in Google Cloud
- Enabling the Search Console API and obtaining authentication keys
- Setting up authentication in Google Colab
Let's dive in!
STEP 1. Create a Project in Google Cloud
First, access the Google Cloud Platform and create a new project.
STEP 2. Enable the Search Console API
After creating your project, you'll need to enable the Search Console API and set up authentication credentials.
STEP 3. Create an OAuth Consent Screen
Next, you'll need to configure the OAuth consent screen to set up authentication for your application.
STEP 4. Create Credentials
Now create OAuth 2.0 credentials to obtain the Client ID and Client Secret needed for API authentication.
STEP 5. Load in Google Colab
# Install required python packages
!pip install oauth2client
!pip install google-api-python-client
!pip install httplib2
# Import required packages
from oauth2client.client import OAuth2WebServerFlow
from googleapiclient.discovery import build
import httplib2
CLIENT_ID = "★Paste your Client ID from Google Cloud here★"
CLIENT_SECRET = "★Paste your Client Secret from Google Cloud here★"
# Define OAuth scopes with read only access
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
# OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters'
# Redirect URI to open Authorization Code Window in Browser
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Build URL that generates Authorization Code
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
# Print Authorization URL
print("Go to the following link in your browser: " + authorize_url)
STEP 6. Authenticate Google Colab and Google Cloud Accounts
Access the authentication URL and copy the Authorization code that appears.
STEP 7. Verify if the Search Console API is Loaded
# Exchange authorization code for credentials
code = "★Paste your authorization code here★"
credentials = flow.step2_exchange(code)
# Create an authorized http object
http = httplib2.Http()
http = credentials.authorize(http)
# Build the service object
webmasters_service = build('webmasters', 'v3', http=http)
# Get list of verified sites
site_list = webmasters_service.sites().list().execute()
# Print the list of sites
for site in site_list['siteEntry']:
print(site['siteUrl'])
STEP 8. Extract Daily Data
# Define the site URL you want to analyze
site_url = 'https://your-website.com/'
# Define the query parameters
request = {
'startDate': '2023-01-01',
'endDate': '2023-01-31',
'dimensions': ['date', 'query'],
'rowLimit': 25000
}
# Execute the request
response = webmasters_service.searchanalytics().query(siteUrl=site_url, body=request).execute()
# Print the results
for row in response['rows']:
print('Date: {}, Query: {}, Clicks: {}, Impressions: {}, CTR: {}, Position: {}'.format(
row['keys'][0], row['keys'][1], row['clicks'], row['impressions'], row['ctr'], row['position']))
Conclusion
This guide covers the basic setup for accessing the Google Search Console API with Python. In future updates, we'll explore more advanced data analysis techniques. I hope you found this tutorial helpful!