Using Python to automate your link building processes

Using Python to automate your link building processes

Using Python to automate your link building processes

There are several methods of building links however most of them involve outreach. Reaching out to press sites in your niche has been a major way for businesses to build backlinks as well as for PR purposes.

Link building involves the following steps:

Getting a list of prospective sites
Getting the email of the editor or  journalist
Sending them the email

To do 1 and 2 above you can employ various techniques and tools. A common way of getting press sites that might be interested in your story is to find press sites that have published a related article. For example, let us assume you are a startup in New York and you are looking for press sites. You can use the “site:” search command.

Using the site command to find press sites that have written about Startup in New York

Using the site command to find press sites that have written about Startup in New York

This will give you a list of press sites that have written about New York startup news and in turn, might be open to write about your startup in New York. You can export this list using the Mozbar chrome extension to google sheets.

The next step is to verify these sites and get the email addresses of the site owners or journalists associated with this site. and are both useful tools to find email addresses. Alternatively, you can go through the “Contact Us” page on the site to get the contact email.

Once you have populated the list of sites and the email address, the next part involves using python to carry out your outreach techniques.

For the purpose of this tutorial we will be making use of the following libraries:
Smtplib – This is to enable us to send out emails
Pygsheets – This is to allow us to read  google sheets directly from the code
Pandas – Although this isn’t necessary, I enjoy using a data frame to be able to access the different headers in my dataset.
Random – We will vary some paragraphs in our email template.
Time – We will be adding a time sleep between the sending of each email.
Email- This package helps us in formatting and preparing our emails. This library won’t help us send emails.

Now that we know the libraries we will be using, next to dive into the code.
Firstly, we will need to fetch the sheets that has the information we need. My sheet looks like this, however you can create as many columns you need with the different information you want to add to the email. I added dummy information to my sheet.

A Google sheet to keep track of the outreach

A Google sheet to keep track of the outreach

Using pygsheets we can fetch the data from the sheet. The title of my sheet here is “Sheet1”. You can give your sheet any title here. The next thing, we do is convert the sheet into a pandas Dataframe. You can read more about  this powerful library  from the official documentation here

The next, we will write a for loop to loop through our data frame and in this block of code we will do all the heavy lifting of sending the email.

The “sent” variable that is an empty an array is so that we will update our sheet with the sent status after sending the email. We also put our login details; email address and password.

I went further to create an array with different introduction and closing paragraphs. With the random library, I randomize the introduction and closing paragraphs each .

We write our for loop and put an if statement to check the status of the email. “Status” here refers to if the email has been sent or not. If it is empty we know we haven’t sent an email to the email address.

The “text” variable contains the content of the email. The first line is the subject of the email. In the curly braces {} we add variables such as the name, url and any other variable you want to add in your email.

Next, we use the Email library to format our email. We assign our  “from” email address as well as the email addresses we want to send the emails to.

The SMTP server here is going to be your server, it could be your localhost. Whatever server you use to ensure you have the right port which is the second argument in smtp.SMTP_SSL . We then use s.login to log into our email account(you need to ensure the email address and password are written correctly). Using sendmail function we parse every variable we want to use in sending the email. This accepts the To and Sender email as well as helps in the formatting of the body of the text. The variables in the curly brackets are passed here so as to ensure they are outputted correctly. The quit function is done at the end of the email code to ask the server to close the existing connection. However, we add a time.sleep command so that it takes 30 seconds before it sends the next email. You do not want to send all the emails at once and you probably do not want to send to more than 100 emails at the same time.

Finally, we update the column with the sent status, which is, in this case, is “Sent”, we do this using the pygsheets library.

Congratulations! You have sent out your first email outreach to a bunch of press sites. However, you can take this a bit further and send out follow up emails to sites that didn’t respond.

You will have to manually update the Responded column with “yes” or “no”, so you will be able to send emails to only sites that didn’t respond to your earlier email.

Next, we use the elif command to add the next conditional statement which is sent only to sites with Response = No and the Status =sent.

The rest of the code doesn’t differ much from what we had in the if statement block of code. The body of the email changes to show this is a follow-up email.

Once you are done with this script you can begin to automate your outreach strategies and scale it!

Note: So as not to face encounter any encoding problem when trying to run the script add the following line of code at the top of your script.

The complete code looks like this

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

6 Best Website Optimization Tools

To assist in your choice of tools towards achieving your desired goals, we’ll be discussing some of the top-notch, most utilized, and most ...

How I Built an Admin Dashboard with Python Flask

In this article, I will share the how I built an admin dashboard. I will give a step by step guideline on how I built this application with Python ...

How to customize exceptions in python

In this tutorial we will look at the purpose of exception, types of exceptions, why it is important to include exceptions in your python programs and ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy