Sub topic 5
Finding broken outbound links on relevant pages
This chapter assumes you know what a backlink is, can read an HTML anchor tag, and recognize the status codes 200, 301 and 404. It covers how to find pages worth checking and how to test their outbound links in bulk.
Start with relevant pages
A broken link only helps you if the page that holds it is relevant to your content. Look for pages that already link out to resources on your topic, such as resource lists, "useful links" pages, and long guides with many citations.
Search operators help here. The third query below finds resource pages on a topic.
intitle:resources "web analytics" inurl:linksCollect a list of candidate pages first. Then check each page's links.
The check loop
The process has four steps for each page.
- Download the page's HTML.
- Extract every anchor
hrefand turn relative paths into full URLs. - Request each URL and record the status.
- Keep the links that count as broken.
A working script
The script below fetches a page and tests each link. It uses a HEAD request first, because HEAD returns only headers and is faster. Some servers reject HEAD with 403, 405 or 501, so the script retries those with GET.
import requests
from urllib.parse import urljoin, urlparse
from bs4 import BeautifulSoup
HEADERS = {"User-Agent": "link-audit/1.0"}
def find_broken_links(page_url, timeout=10):
html = requests.get(page_url, headers=HEADERS, timeout=timeout).text
soup = BeautifulSoup(html, "html.parser")
broken = []
seen = set()
for a in soup.find_all("a", href=True):
url = urljoin(page_url, a["href"])
if urlparse(url).scheme not in ("http", "https") or url in seen:
continue
seen.add(url)
try:
resp = requests.head(url, headers=HEADERS, allow_redirects=True, timeout=timeout)
if resp.status_code in (403, 405, 501):
resp = requests.get(url, headers=HEADERS, stream=True, timeout=timeout)
resp.close()
status = resp.status_code
except requests.RequestException:
status = None
if status is None or status in (404, 410) or status >= 500:
broken.append((url, status))
return broken
if __name__ == "__main__":
for url, status in find_broken_links("https://example.com/resources"):
print(status, url)Key details in the code:
urljoinresolves relative links such as/guideagainst the page URL.- The scheme check skips
mailto:andjavascript:links. - The
seenset avoids testing the same URL twice. allow_redirects=Truefollows 301 and 302 responses, so a moved page is judged by its final status.- A status of
Nonemeans the request failed to connect, which usually means a dead domain.
Common errors
| Problem | Effect | Fix |
|---|---|---|
| Treating 403 as broken | False prospects, since many sites block bots | Retry with GET, then check by hand |
| Ignoring timeouts | Slow servers look dead | Retry once before you report |
| Missing soft 404s | A page returns 200 but shows "not found" | Spot-check pages that redirect to a home page |
| Checking too fast | Your IP gets blocked | Add a delay between requests |
Prioritize what you find
Not every broken link is worth pursuing. Prefer links on pages that are relevant, have real traffic and are kept up to date. A dead link in a ten-year-old forum thread will rarely lead to a placement.
Exercise
Your script reports a 403 for a link. Why should you not treat it as broken straight away, and what should you do?
Show answer
Many servers return 403 to automated requests while serving real visitors normally. Retry with a GET request, then open the link in a browser. Only treat it as broken if the page really fails to load.
Exercise
A page returns status 200, but the text says "Sorry, this page no longer exists." What is this called, and how would you catch it?
Show answer
It is a soft 404. Status codes alone will not reveal it, so check the page content for phrases like "not found", or check whether the URL redirected to a home page.