Getting DA and PA using the Mozscape Links API w/ Python

Share this:

As anyone who has read this blog before will know, I often like to browse through Reddit sometimes for inspiration on content ideas.

This post is the latest in what I’ll start referring to as my “I was bored, so found something on Reddit to code” series—a working example of how to programmatically get Domain Authority (DA) and Page Authority (PA) metrics using the Mozscape Link API.

For anyone who is interested, the original post from user u/Financial-Regret-512/ on r/SEO is embedded below:

It’s fair to say I cheated on this one—as I’d already written most of the script a few nights before, as part of the first iteration of my script to scrape guest post opportunities, which uses a SERP API to pull URLs from Google and the Mozscape Links API to retrieve their metrics.

There was just a little reworking that needed to be done, which I’ll go over in the post that follows—but first, I’d like to get a point about third-party metrics out of the way, as there are plenty of SEOs out there who aren’t the biggest fan of these, DA and PA in particular.

The elephant in the room…third-party metrics

I suppose the best place to start on this topic is the elephant in the room, as I’m sure I’ll have someone from SEO Reddit will sneak into the comments to point out that…

Yes, DA and PA are third-party metrics—one of several. This means that as metrics, they don’t really count for much, so don’t rely on them as a measure of whether or not a link from a given site is going to be worth much.

That said, when used in conjunction with other factors, like a site’s rankings, traffic, and other more “real” metrics, they can be somewhat useful in assessing the value of a link from a site, but not on their own.

These metrics from Moz seem to be particularly not well liked—at least from what I’ve seen online—and you don’t have to go too far to see what I mean, just look at the comments of the post I’ve embedded above.

Things you’ll need

To use this script, you’re going to need to signup for a free account to access the Mozscape Links API. You can sign up for an account here.

One thing to note is that while an account is free, for some reason Moz insists on you using a credit card to sign up—which as anyone whose come across my no credit card SEO free trials round-up will know, I’m not the world’s biggest fan of.

Beyond that, you’ll just need to have Python installed and ready to go—if you haven’t, refer to the official getting started guide.

The code

There’s not a lot to this one, so I just chucked everything in the same file—not really enough here to warrant piecing it out into separate files. That said, I’ll briefly walk you through what each of the functions does.

  • chunk_list()—breaks your list of URLs down into chunks of up to 50. This is due to the limit on how many URLs you can query with the Mozscape Links API at a time.

  • moz_links_api()—should be self-explanatory, this is where the request is made to the Mozscape API, returning Domain Authority (DA) and Page Authority (PA) metrics.

  • write_csv()—writes the result to a csv file, ready for you to use in Google Sheets, Excel, etc.

  • main()—this loops through the URLs, running each one through the moz_links_api function before writing the result to csv using the write_csv function.

index.py

import requests
import time
import csv

moz_access_id = "mozscape-XXXXXXXXXX"
moz_secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

def chunk_list(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

def moz_links_api(urls, access_id, secret_key):
    auth = (access_id, secret_key)
    request_url = "https://lsapi.seomoz.com/v2/url_metrics"
    data = """{
        "targets": ["""+urls+"""] 
    }"""
    request = requests.post(request_url, data=data, auth=auth)
    moz_data = request.json()
    all_site_metrics = []
    if "results" in moz_data:
        for site_metrics in moz_data["results"]:
            site_metrics_row = {"url": site_metrics["page"].rstrip('/'),"domain_authority": site_metrics["domain_authority"], "page_authority": site_metrics["page_authority"]}
            all_site_metrics.append(site_metrics_row)
        return all_site_metrics

def write_csv(data, filename):
    file = open(filename, 'w', newline='')
    writer = csv.DictWriter(file, fieldnames=['url', 'domain_authority', 'page_authority'])
    writer.writeheader()
    writer.writerows(data)
    file.close()
    print("written to csv file...done!")

def main():
    file = open("file.txt", "r")
    data = file.read()
    urls = data.split('\n')
    chunked_list = list(chunk_list(urls, 50))
    urls_with_metrics = []
    for chunk in chunked_list:
        url_list = []
        for url in chunk:
            url_list.append(url)
        url_string = ''.join(f'"{url}", ' for url in url_list)
        moz_metrics = moz_links_api(url_string, moz_access_id, moz_secret_key)
        if moz_metrics:
            for url in moz_metrics:
                urls_with_metrics.append(url)
            time.sleep(10)
    write_csv(urls_with_metrics, 'urls_with_metrics.csv')

if __name__ == "__main__":
    main()

How to use it

1. The first thing you’ll need to do is switch out the placeholder for your actual Mozscape API credentials, putting them into the script as seen in the snippet below:

index.py

moz_access_id = "mozscape-XXXXXXXXXX"
moz_secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 

2. Then you’ll create a txt file of all the URLs you want to run through the Moscape Links API. In the example, this file is named “file.txt“, but to change it simply edit the part of the code in the main() function, as shown in the snippet below:

index.py

file = open("file.txt", "r")
data = file.read()
urls = data.split('\n')

3. Open up a terminal, navigate to the folder you put the script and file containing your text file of URLs, and run “py index.py“—once it’s done, you’ll end up with a csv file with all your Domain Authority (DA) and Page Authority (PA) metrics, per URL.

Summing it up

Well that’s it—a little example of how to use the Mozscape Links API in Python. There’s not a lot to this one, and as I stated earlier in the post, the main reason I threw this together is that I already have 90% of it ready to go from a post last week.

Not everyone is a fan of third-party metrics—and sure, they’re right that they don’t really tell you much. Personally, I find them useful as part of a review process when looking at backlink targets, but I certainly wouldn’t rely on them alone. Sure, they can be useful—just don’t put too much stock in them.

While this was an example for using the Mozscape API, this could just as easily to repurposed for any third-party SEO tool API—so if you’re new to coding, hopefully you’ve got something out of this that can help you as you build up your skills and get stuck in.

If you made it this far, thanks for reading! And as always, if you’ve got any thoughts, views, pointers, or pretty much anything else constructive to say, feel free to drop these in the comments below.

Share this:

Leave a Comment