In this post, I’ll show you how to boost your SEO with a mostly automated internal linking strategy using Python and Google Sheets.
Update: try the web app version of this script!
Linking one page to another is an important task for any SEO project.
Internal linking provides users with related pages they can visit to keep them exploring your website, so they are great for users.
Google also loves internal links. When you link one page to a related page, it passes authority to that page and helps Google better understand the structure of your website.
How internal linking works for SEO
Internal linking is one of the best growth levers for SEO. It has a direct impact on rankings. SEJ recently did a review and confirmed internal links are a ranking factor.
Linking passes authority, or as Google calls it, PageRank. So if you want to boost up a valuable page you could add some links to it from related pages.
Or vice versa, if you want to boost a blog post you could send it some links from a relevant higher traffic page to pass some “link juice” to it.
Using the right anchor text for links is also important. You don’t want to be too aggressive with keyword usage, but you also don’t want to be too unspecific.
There’s a lot of nuance to a good internal linking strategy. Check out this in-depth internal linking guide by Backlinko to learn more about the strategy behind interlinking.
What a typical internal linking process looks like
I’ve been linking pages for more than 10 years. I’ve found that it’s often a time-consuming process that requires lots of manual selection and effort.
There are some platforms and plugins that can help automate the process, but I’ve found that they are often just as time-consuming to set up and figure out.
One of the areas I find myself spending the most time on is adding new links to existing pages to boost rankings. Which is why I want to automate it.
Here’s what my manual internal linking process looks like:
- Identify the page I want to build links to.
- Use Google search with the “site:” modifier +keyword, which provides a sorted list of your most relevant pages for that search term.
- Check those pages and scan the content to see if they already link to my target page.
- Link a few pages to the target page with varied anchor text.
The benefit to this method is that you are using Google’s index of your website to find what pages Google thinks are most relevant.
How internal linking works with Python
I essentially figured out how to do this manual process at scale with Python.
I can then use ChatGPT to provide anchor text recommendations if needed.
Here’s what my automated internal linking process looks like:
- Create a sheet of pages and target keywords.
- Use a Python script to search Google and recommend the top pages to link to. (Excluding pages that already link to the target page.)
- The Python script appends your sheet with internal link options in the adjacent columns.
- Bonus: Use ChatGPT to add anchor text recommendations.
This automated version is basically the same manual process, just done all at once on a spreadsheet.
I can use the output sheet as a reference and plug the links in, which can save hours on internal linking projects.
How to automate internal linking with Python step-by-step
It only takes a few minutes to create a detailed interlinking plan for your website. Here’s how.
Step 1: Create a sheet of pages and target keywords.
This step is pretty straightforward. You need a sheet that lists the pages you want to build links to, and the target keyword.
The target keyword is what Python will search Google for with the “site:” modifier to find relevant pages.
Here’s an example for Nerdwallet:
In most cases, the keyword should be the primary target keyword of the page. The search method we will use is flexible and will show pages that contain the keyword exactly or very closely.
Be sure to use the column headings “target_page” and “keyword” otherwise the script won’t work.
Your list can be a handful of pages or all your pages. It’s up to you.
Once you have the list, export it as a CSV.
Step 2: Grab my internal linking Python script
Next, you need Google Collab and this Python script. Open up this Google Collab link and make yourself a copy of this script.
You will need to specify the domain and add your own Google API key. More on that in the next step.
Step 3: Register a custom search engine
Next up, Google doesn’t like it when scripts like Python try to scrape search results.
Luckily, you can register your own custom search engine for free in a few clicks and get an API key for it.
Leveraging Google’s own search functionality to find relevant sites to link to instead of trying to scrape search results is a great workaround.
Getting an API Key from Google Cloud is straightforward. Here is the step-by-step process:
1. Go to Google Cloud Console:
First, you’ll need to go to the Google Cloud Console (console.cloud.google.com) and sign in with your Google Account. If you don’t have a Google Cloud account, you’ll need to create one and set up a billing account even if you’re planning to use a free service.
2. Create a new project:
In the Cloud Console, you’ll see a dropdown at the top of the page next to “Google Cloud Platform”. Click on it to create a new project. You can name it whatever you like, and then click “Create”.
Enable the Custom Search JSON API for your project: Go to the “Library” section in the Cloud Console. Here, you can see all the available APIs. Search for “Custom Search JSON API” and enable it for your project.
3. Create the API key:
After enabling the API, go to the “Credentials” section using the navigation menu on the left. Click “Create credentials” and then choose “API Key”. Your new API key will be created and displayed in a pop-up window.
4. Restrict the API key (optional but recommended):
In the window that displays the API key, you can click “Restrict Key” to add restrictions for security. You can choose to restrict the key by HTTP referrer, IP address, or by the type of API it can call. If you’re just using the key for the Custom Search JSON API, you can choose to restrict it to that API.
Additional considerations for the API key:
- Remember to replace ‘YOUR_API_KEY’ in your code with the key you generated.
- Remember to secure your API key. Don’t share it with others or post it publicly. If the key is compromised, others could use it, potentially leading to charges on your billing account.
- Please note that using Google’s Custom Search JSON API isn’t free, so ensure you’re aware of the costs before you proceed.
How to get A Search Engine ID
To obtain a Custom Search Engine ID (CX), you need to create a Custom Search Engine (CSE) on Google. It’s really easy. Here’s how you do it:
- Go to the Google Custom Search home page (cse.google.com).
- Click on “New search engine” on the left and enter the necessary information.
- In the “Sites to search” box, put the website(s) you want the custom search engine to search.
- Click “Create” at the bottom of the page.
- After your CSE is created, you can access it from the control panel. On this page, you can customize the search engine (look and feel, search features, etc.) if needed.
- Your Search Engine ID (CX) can be found in the Basics tab under “Details”. It’s listed as “Search engine ID”.
Remember to replace ‘YOUR_SEARCH_ENGINE_ID’ in your Python code with this ID. This will allow you to use your Custom Search Engine with the Google Custom Search JSON API.
Note that there are usage costs associated with Google’s Custom Search JSON API. It may cost a few cents to run this.
Step 4: Run the script
Once you plug in your domain, API key, and Search Engine ID the script is ready to run.
Select Runtime > Run all and the script will get to work.
After it installs the libraries and is ready to go, scroll down to the bottom. It will have a prompt asking you to upload your CSV file.
After you upload your CSV, it will start finding linking opportunities for each page in the sheet. When complete, it will automatically download the new CSV file to your downloads folder.
It uses the search modifier “site:yourdoman.com +keyword”, then finds pages, and checks if they already link to the page.
It excludes pages that are already linked. So it’s the same process as doing it manually, but it will do it in bulk for all URLs in your sheet in a few seconds.
Step 5: Upload your internal linking sheet
Now you just need to upload your new sheet as a new tab. Here’s the output I got for my Nerdwallet links:
It gives me 10 pages per URL with link opportunities.
Here’s an example for the first one with the keyword “Get Out of Credit Card Debt”. There are a few spots where we can easily add an internal link to the target page.
With 10 link options per target page, I have tons of opportunities to add some relevant internal links.
Bonus: Use Chat GPT for anchor text recommendations
Now that you have a sheet of links, you just need to plug some in. With 10+ options per page, it should be easy to add a few relevant links to the target page.
I you want to mix up your anchor text and you’re not sure what exact anchor text to use, ChatGPT can help.
Just ask ChatGPT for 10 anchor text ideas for your target URL:
Give it a go
That’s all there is to it. Now you can automate your next internal linking project.
Try it out today and see how much time it can save you.
Connect with me on LinkedIn if you have questions or want to share your thoughts or ideas!