Blog / Build with AI /
Building an internal link finder script with AI
Internal links are the cheapest SEO work there is, and the work most people never get round to. You publish a new post, you tell yourself you will link to it from older pages, and three months later nobody has.
The problem is not knowing links matter. It is the grind of finding the right spots. On a site with 200 pages, who remembers what is on page 143?
Here is a small tool that does the finding for you. It reads your sitemap, fetches the pages, and suggests where one page could link to another. A person still decides. That part matters, and I will come back to it.
The finished thing, in one line
A Python script that takes a target page and prints a ranked list of other pages that mention its topic but do not link to it yet.
That is all. It does not edit your site. It produces a list you review by hand.
What you need
- Python 3 installed
- A sitemap URL (most sites have
/sitemap.xml) - An AI chat tool to write and explain the code
- About an hour, the first time
If you have never run a script, running Python scripts without being a developer walks through the basics.
The logic, in plain words
Think of a librarian who has read every book in the building. You hand them one book and ask, “Which other books talk about this subject but never refer to it?” That is the script.
Three steps:
- Collect the pages. Read the sitemap and get every URL.
- Read each page. Fetch it and pull out the main text and the links already on it.
- Match. For a target page, look for other pages whose text contains the target’s key phrase and which do not already link to it.
No machine learning is needed for a first version. Phrase matching is crude but surprisingly useful.
Step 1: describe the tool before you ask for code
Do not open with “write me an internal link tool”. You will get something generic and half-wrong. Spend two minutes on a clear description, as the post on how to describe a tool to AI so it builds the right thing suggests.
I want a Python script. Input: a sitemap URL, a target page URL, and a key phrase. It should fetch every URL in the sitemap, read the visible text of each page, and list the pages that contain the key phrase but do not already contain a link to the target page. Use the requests and BeautifulSoup libraries. Print the results as a numbered list with the URL and a short snippet around the phrase. Add a one-second pause between requests. Explain each part in comments.
Notice the details. The libraries are named. The delay is specified. The output format is clear. Each detail removes a guess.
Step 2: run it on a small site first
Ask the AI to limit the first run to ten URLs. A bug that hammers your whole site with requests is a bad way to learn. Once the output looks sensible, remove the limit.
If something fails, paste the exact error back, not a description of your mood. Debugging with AI: give it the error, not the frustration explains why.
Step 3: make the matching smarter
The first version only matches one exact phrase. Real pages vary. Ask for improvements one at a time:
Update the script so it also matches simple variations of the phrase: plural forms, and the words in a different order within the same sentence. Keep the old behaviour as an option.
Then:
Skip links in the navigation, footer and sidebar when deciding whether a page already links to the target. Only look at the main content area.
That second one matters. A link in the site-wide menu does not count as a contextual link in the article. Without this, the script will wrongly think half your pages already link where you want.
Small steps beat big rewrites, as iterating with AI: small changes beat big rewrites argues.
Step 4: add anchor text suggestions
A list of pages is useful. A suggested sentence is better. Ask the script to show the sentence where the phrase appears, so you can see whether a link would read naturally.
For each match, print the full sentence containing the phrase, with the phrase in capital letters.
You will quickly see which suggestions are good. A phrase sitting in a sentence about something else is a bad place for a link. This is why a person reviews the list.
Where it goes wrong
It suggests links that make no sense
Phrase matching knows words, not meaning. “Apple” in a recipe and “Apple” in a phone review look the same to it. Treat every suggestion as a candidate, not an instruction.
JavaScript-rendered pages come back empty
A basic fetch only gets the HTML the server sends. If your pages build their content in the browser, the script will see little. For those sites you would need a different approach, such as a rendering tool, which is a bigger job.
Too many links on one page
Just because you can add ten links to an old post does not mean you should. Pick the two or three where the reader benefits. Links added for the sake of links read as such.
Being rude to your own server
Always keep the delay. A script that fires hundreds of requests at once can slow a small hosting plan to a crawl.
Make it honest about what it cannot know
The script has no idea which pages matter most. A link from a page nobody visits does little. If you have traffic data, export the top pages from your analytics and ask the AI to put those at the top of the suggestions. Your highest-traffic pages are the best places to send readers onward.
It also cannot tell which pages are outdated, thin or about to be deleted. Do not link to pages you would not recommend.
A word on checking AI-written code
You are fetching pages from a live site, so read the code before running it. At minimum, confirm it only makes GET requests, only visits your own domain, and writes nothing except its output. Copying code from AI safely has a short list for this.
Keep a record
When you accept a suggestion, note it in a spreadsheet: date, source page, target page, anchor text. Otherwise the next run will suggest the same links again, and you will have forgotten what you already did.
Key takeaways
- Internal linking is mostly a finding problem, and a short script handles the finding well.
- Describe inputs, outputs, libraries and limits in your first prompt.
- Test on ten URLs before running on the whole site.
- Exclude navigation and footer links, or the results will mislead you.
- Use the output as a shortlist. You decide what gets linked.
FAQ
Do I need to know Python?
Not much. You need to be able to run a script and read an error message. The AI writes the code, and you ask it to explain anything unclear.
Will this work on Shopify?
Yes, for reading public pages. Remember the script only suggests; you make the changes in the store yourself.
How many links per page is right?
There is no magic number. Link where it helps the reader, and stop when it stops helping.
Related reading
Next step: pick one page you want to push, write down its key phrase, and run the script against ten URLs. What does it find that you had missed?