
Using Python to Track and Analyze Keyword Rankings Automatically
As a digital marketer or SEO specialist, monitoring keyword rankings is an essential task to gauge the performance of your website’s online presence. Manually tracking keyword positions can be time-consuming and labor-intensive. However, with Python, you can automate this process using various libraries and APIs. In this article, we’ll explore how to use Python to track and analyze keyword rankings automatically.
Prerequisites
Before diving into the implementation, ensure you have:
- Python 3.x installed on your system.
- pip, the package installer for Python.
- A Google account with access to Google Search Console (GSC) API or another third-party keyword tracking tool.
Required Libraries
For this project, we’ll need the following libraries:
google-api-python-client
for interacting with Google APIsgspread
for reading and writing data from Google Sheetspandas
for data manipulation and analysisschedule
for scheduling tasks
You can install these libraries using pip:
markdown
pip install google-api-python-client gspread pandas schedule
Step 1: Set up Google API Credentials
To use the GSC API, you’ll need to create credentials for your Python application. Follow these steps:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Navigate to APIs & Services > Dashboard and click on Enable APIs and Services.
- Search for the Search Console API, select it, and click on Enable.
- Go to APIs & Services > Credentials and click on Create Credentials > OAuth client ID.
- Select Other as the application type and give a name to your client ID.
- Download the JSON key file and save it securely.
Step 2: Set up Google Sheets
Create a new Google Sheet where you want to store your keyword rankings data. We’ll use this sheet to read and write data from our Python script.
Step 3: Authenticate with Google API
Use the google-api-python-client
library to authenticate with the GSC API:
“`python
import os
from googleapiclient.discovery import build
Load credentials from JSON key file
creds = None
if creds is None or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
creds = Credentials.from_authorized_user_file(
‘token.json’, scope=SCOPES)
service = build(‘searchconsole’, ‘v1’, credentials=creds)
“`
Step 4: Track Keyword Rankings
Use the gspread
library to read and write data from your Google Sheet. We’ll create a function to track keyword rankings:
“`python
import gspread
def get_keyword_rankings():
# Open the Google Sheet
gc = gspread.service_account(filename=’path/to/credentials.json’)
sheet = gc.open(‘Keyword Rankings’).sheet1
# Fetch data from GSC API and write it to the sheet
service = build('searchconsole', 'v1')
request = service.searchconsole().get_keyword_positions(
body={'keyword': 'example keyword'})
response = request.execute()
# Write data to Google Sheet
sheet.update_cell(1, 1, 'Keyword')
sheet.update_cell(1, 2, 'Ranking')
for result in response['result']:
row_num = len(sheet.get_all_values()) + 1
sheet.update_cell(row_num, 1, result['keyword'])
sheet.update_cell(row_num, 2, result['position'])
“`
Step 5: Schedule the Task
Use the schedule
library to schedule the task to run at regular intervals:
“`python
import schedule
def track_keyword_rankings():
get_keyword_rankings()
Run the task every hour
schedule.every(1).hour.do(track_keyword_rankings)
while True:
schedule.run_pending()
time.sleep(1)
“`
Conclusion
In this article, we’ve shown you how to use Python to track and analyze keyword rankings automatically using various libraries and APIs. By setting up Google API credentials, authenticating with the GSC API, tracking keyword rankings, and scheduling the task, you can save time and effort in monitoring your website’s online presence.
Feel free to modify this code to suit your specific needs and use cases. Happy automating!