In this post I am going to show you how you can extract the page authority and the domain authority among other metrics from MOZ in a bulk mode by using its API. As well as the domain authority, we will also be able to fetch the following metrics with the freemium version:

  • Page metatitle if available.
  • Equity links: meaning those links that pass page rank.
  • Total number of links.
  • URL response code.
  • Page Authority.
  • Domain Authority.
  • Timestamp: when the URL was last crawled.

The final output of this script will look like:

It is worth mentioning that Moz API has up to 2.500 requests for free and you can sign up over here if you do not have an account yet. On this article written by Yanis Illoul you can find more information about how you can get your key to make use of the API.

Does this sound interesting? Let’s get started then!

1.- Installing the library

First of all you need to run the following command in your terminal to install the library Mozscape:

pip install git+https://github.com/seomoz/SEOmozAPISamples.git#egg=mozscape&subdirectory=python

However, unfortunately I encountered some issues and I did not manage to install the library so what I did is executing the piece of code that defines the Mozscape function before making the request to the API in the notebook. The piece of code that I ran can be found here.

2.- Making the request

Now that we have either installed or defined our function, we can make the request and fetch the metrics from MOZ in bulk mode. Nevertheless, first of all we will import the list of domains that we want to check from an Excel file and we will create a list with them. In order to import the domains from the Excel file I am going to use Pandas:

import pandas as pd

df = pd.read_excel ('Domains.xlsx')
names = [[x] for x in df.values.tolist()]

When the domains are already imported and stored in the list, we can just iterate over the list, make the request to the API endpoint and append the API response with the metrics for each domain. Sometimes the API gets overloaded if many requests are made, so we will introduce a try and except and it will sleep for 10 seconds in case an error is returned by the API. You will need to add your key and Moz account in this piece of code:

import time

client = Mozscape('<your-moz-account>', '<your-key>')


for x in names:
    try:
        print(x[0])
        domainAuthority = client.urlMetrics(x[0])
        x.append(domainAuthority)
    except Exception as e:
        print(e)
        time.sleep(10)
        domainAuthority = client.urlMetrics(x[0])
        x.append(domainAuthority)

Once we have got all the metrics, we will format them and we will export them as an Excel file. We will need to remove some deprecated metrics that are returned by the API with the method pop and we will also adapt the timestamp since the API gives it in unix format.

import datetime

list_values = [list(x[1].values()) for x in names]

for y in list_values:
    y[11] = datetime.datetime.fromtimestamp(y[11]).strftime('%Y-%m-%d %H:%M:%S')
    y.pop(4)
    y.pop(4)
    y.pop(4)
    y.pop(4)

Finally, we can export it as an Excel file again with Pandas:

import pandas as pd
pd.DataFrame(list_values).to_excel("MOZ_results.xlsx", header=["Title","URL","Equity Links","Links","Response Code","PA","DA","Last Crawled"], index=False)

This will create an Excel file that will look like the one above. That is all folks, I hope that you found this post interesting!