brightlocal api python

Use Python and Brightlocal API to Grab Your Keyword Rankings

Estimated Read Time: 3 minute(s)
Common Topics: api, data, brightlocal, python, keyword

BrightLocal is a common tool for local SEO, citations, and ranking tracking. It offers an extensive API that lets you retrieve data and automate many tasks. In this tutorial I show how to fetch ranking data from the BrightLocal API using Python. The BrightLocal API is paid, but it includes 1,000 free calls for testing. That may sound like a lot, but you may need multiple calls—especially if you manage many campaigns. Because the data is proprietary, I will not show example output.

Requirements and Assumptions

  • Python 3 is installed and basic Python syntax is understood.
  • Access to a Linux installation (Ubuntu recommended) or Google Colab.

Import Modules

  • requests for API calls.
  • json for data parsing.
  • pandas for data storage.
  • %load_ext google.colab.data_table — a Colab pandas extension for improved table display.
import requests 
import json
import pandas as pd
%load_ext google.colab.data_table

First, set your API key (found in your BrightLocal account profile). Then create an empty DataFrame to store keyword data.

Set API Key and Dataframe

apikey = "YOUR_API_KEY"
df = pd.DataFrame([], columns=['CID','Keyword','Current Google','Last Google'])

Make the first API call to retrieve all campaign IDs in your account. Use these IDs in a second call to get rankings for each campaign’s tracked keywords. To process only selected campaigns, replace the automatic list with a manual list of campaign IDs and loop through those.

Setup First API Call

url = "https://tools.brightlocal.com/seo-tools/api/v2/lsrc/get-all?api-key=" + apikey 
payload = {} 
headers = {} 
response = requests.request("GET", url, headers=headers, data = payload) 
camids = json.loads(response.text.encode('utf8'))

Below we parse the JSON response and build the campaign ID list. A counter prints the number of campaigns found.

Build Campaign List

ids = []
counter = 0
getcam = camids["response"]["results"]
for i in getcam:
  ids.append(i["campaign_id"])
  counter = counter +1
print(ids)
print(str(counter) + " campaigns")

Then loop through the campaign IDs and make another API call to retrieve rankings for each campaign’s tracked keywords. The example captures current and previous Google rankings, which lets you compute differences. The JSON also contains current and previous rankings for Microsoft Bing and can be accessed using the same pattern.

For example, use kdata["response"]["result"]["rankings"]["rankings"][i]["bing"][0]["rank"] for the current Microsoft Bing ranking.

Get Rankings

  for id in ids:
    url = "https://tools.brightlocal.com/seo-tools/api/v2/lsrc/results/get?api-key="+apikey+"&campaign-id=" + str(id)
    payload = {}
    headers = {}
    response = requests.request("GET", url, headers=headers, data = payload)

    kdata = json.loads(response.text.encode('utf8'))
    
    getkeywords = kdata["response"]["result"]["rankings"]["keywords"]

    for i in getkeywords:
      cgoogle = kdata["response"]["result"]["rankings"]["rankings"][i]["google"][0]["rank"]
      try:
        lgoogle =  kdata["response"]["result"]["rankings"]["rankings"][i]["google"][0]["last"]
      except:
        lgoogle = "n/a"

      keyword_dict = {"CID":id,"Keyword":i,"Current Google":cgoogle,"Last Google":lgoogle}
      df = df.append(keyword_dict, ignore_index=True)

df.head(25)

Conclusion

You can now see how straightforward it is to retrieve keyword ranking data from the BrightLocal API. From here, automate, store, and extend the script to collect more data or perform calculations. Please follow me on Twitter for feedback and to see ways to extend the script.

BrightLocal API FAQ

How can I use Python to retrieve keyword rankings using the BrightLocal API?

You can use Python scripts to call the BrightLocal API and retrieve keyword ranking data for analysis and reporting.

What Python libraries are commonly used for making API requests to BrightLocal and fetching keyword rankings?

The requests library is commonly used to make HTTP requests to the BrightLocal API and retrieve keyword ranking information.

What specific keyword ranking data can be obtained using Python and the BrightLocal API?

You can extract ranking position, search volume, and other SEO-relevant metrics.

Are there any considerations or limitations to be aware of when using Python and the BrightLocal API for keyword rankings?

Be aware of API rate limits, authentication requirements, and BrightLocal’s usage policies when fetching keyword rankings.

Where can I find examples and documentation for using Python with the BrightLocal API to grab keyword rankings?

See the BrightLocal API documentation for guides and examples. Also consult online tutorials and Python resources for practical demonstrations and implementation details.

Greg Bernhardt
Follow me