• Python

How To Build A Rank Tracker Using Python & Google Search API

  • Felix Rose-Collins
  • 4 min read

Intro

Even with the significance of keyword tracking, Google’s ToS (Terms of Service) prohibit scraping SERPs (Search Engine Results Pages).

Google’s anti-scraping measures like CAPTCHAs and IP blocking drive up CAPTCHA-solving services costs, including the cost of rotating proxies. This makes the whole SEO monitoring process through web scraping even more frustrating.

Well, if you are in this situation or struggling to get accurate and legal Google search data to track keyword ranking, here’s a legal and reliable alternative. Let’s explore how to build a rank tracker using Python and Google Search API.

Building a Rank Tracker Using Python & Google Search API

To build a rank tracker using Python and Google Search API, you need skills in Python programming, API handling, database management, and data analytics.

Note that this guide focuses on building a rank tracker for global keyword tracking. For local and advanced keyword tracking, integrating your Python script with a rank tracker API should come in handy.

Let’s now delve into the process of building a rank tracker using Python and Google Search API.

1. Set up your Python programming environment and requirements

Before writing a Python script to track keyword ranking, you must install the latest version of Python from the official website. The installation process varies based on the operating system you are running.

Once installed, verify installation in the command line or terminal. Then, install an IDE or code editor of your choice. Among the IDEs, VS code is most recommended as it supports most Python scripting extensions and it is lightweight.

Meet Ranktracker

The All-in-One Platform for Effective SEO

Behind every successful business is a strong SEO campaign. But with countless optimization tools and techniques out there to choose from, it can be hard to know where to start. Well, fear no more, cause I've got just the thing to help. Presenting the Ranktracker all-in-one platform for effective SEO

We have finally opened registration to Ranktracker absolutely free!

Create a free account

Or Sign in using your credentials

Next, install a virtual environment to separate dependencies of each project you develop, preventing potential conflicts. And, finally, install the necessary Python libraries and tools.

Among Python’s libraries and tools, you need the Requests library to make the API calls to Google Search API, _JSON _to process API responses, Datetime to timestamp queries, SQlite to store ranking data and history, and Plotly or Matplotlib for visualization.

2. Configure the Google Search API

After setting up and testing your Python programming environment, head to the Google Programmable Search Engine page and create a new or custom search engine.

For this tutorial, set your custom search engine to search across the entire web (global searching) and copy its ID or CX value. Then, switch pages to the Google Cloud Console and enable the Custom Search JSON API to obtain an API key.

Upon obtaining the CX value and API key, securely save the two in a configuration file. Always avoid hardcoding the CX value or API key to reduce security risks.

3. Write a well-structured Python script

So far, you have a correctly configured programming environment and the ID (plus API key) of a custom Google search engine.

Here’s how to structure your Python script into modular functions to ensure a smooth and automated keyword rank-tracking process.

First, structure your project to include the _config.py, tracker.py, database.db, and requirements.txt _file for better navigation and maintainability.

Meet Ranktracker

The All-in-One Platform for Effective SEO

Behind every successful business is a strong SEO campaign. But with countless optimization tools and techniques out there to choose from, it can be hard to know where to start. Well, fear no more, cause I've got just the thing to help. Presenting the Ranktracker all-in-one platform for effective SEO

We have finally opened registration to Ranktracker absolutely free!

Create a free account

Or Sign in using your credentials

Don’t forget the data/ folder to hold the stored ranking history. Once these files and the folder are in place, write the following functions to read from, write to, or modify the files:

a) A function to Query Google Search API

This function should accept a keyword as input and construct the API request URL using the CX value and API key from the config.py file. It should then send the request to the Google Search API, which sends back a JSON response.

Lastly, the function parses the JSON response, extracting the titles, URLs, snippets, and ranking positions of the first 100 pages.

b) A function to extract and store rank data

After parsing the search results, this function is to locate your website’s URL in the returned list, determine its ranking position and store the ranking data in a structured format in SQlite.

The function should loop through the search results and find the position of your site’s URL. If found, it should extract and record its ranking position. If not found, it should assume that your site does not rank in the top 100 for the specified keyword.

Meet Ranktracker

The All-in-One Platform for Effective SEO

Behind every successful business is a strong SEO campaign. But with countless optimization tools and techniques out there to choose from, it can be hard to know where to start. Well, fear no more, cause I've got just the thing to help. Presenting the Ranktracker all-in-one platform for effective SEO

We have finally opened registration to Ranktracker absolutely free!

Create a free account

Or Sign in using your credentials

You can have the function structure the data into the following SQlite database fields — keyword searched, data and time of tracking, ranked position, and URL.

c) A function to analyze data and visualize it

Once in the database, this function pulls the stored ranking data from the database, analyzes it and uses libraries like Plotly and Matplotlib to visualize trends. You can also have the model pass the data to statistical models to detect ranking drops and increases, helping you monitor SEO efforts.

1. Automate the Python rank tracker

Test the functionality of your script and implement error handling.

For instance, the script should be able to handle timeouts or retry failed requests. Also, log failed requests to avoid losing data.

To monitor ranking after a specific interval, automate the whole tracking process using a scheduler like cron to automate keyword tracking after a certain interval (how many times per day, week, or the month). Have triggers in place to alert you whenever ranking drops significantly.

2. Add advanced features to your Python rank tracker

Finally, once you are comfortable with building a basic rank tracker, you have the option of adding the following advanced features:

  • Device-specific tracking: Mobile and desktop rankings vary. That’s why the Google Search API allows you to track either rankings separately.

  • Competitor tracking: Other than your website, you can track competitor URLs.

  • Geolocation-based rank tracking: You can narrow down to a specific location and fetch results that reflect the search intentions of the users in a certain city or country.

  • SERP feature detection: If you want to know whether your site pages appear in the image results, featured snippets, or other SERP features, the Google Search API allows you to be this specific.

Despite the advanced features, note that you have about 100 free queries per day. After you’ve depleted this, you have to pay. Also, you can only pull up to 100 search results. That’s why it is essential to combine your rank tracker with a third-party rank tracker API to extend its functionality.

Wrapping Up!

And there you have it! The five steps you need to follow to build a rank tracker using Python and the Google Search API.

Remember, the Google Search API limits your tracking to 100 search results and 100 queries a day. Despite this, you get to efficiently and legally track keyword ranking without IP bans or triggering CAPTCHAs. Plus, you are allowed to distribute requests across multiple accounts, increasing the number of queries you can send within a day.

Felix Rose-Collins

Felix Rose-Collins

Ranktracker's CEO/CMO & Co-founder

Felix Rose-Collins is the Co-founder and CEO/CMO of Ranktracker. With over 15 years of SEO experience, he has single-handedly scaled the Ranktracker site to over 500,000 monthly visits, with 390,000 of these stemming from organic searches each month.

Start using Ranktracker… For free!

Find out what’s holding your website back from ranking.

Create a free account

Or Sign in using your credentials

Different views of Ranktracker app