When you build or buy links, you want to know whether each one is actually live and what rel attributes it carries. Checking pages by hand gets old fast. This post shows a small Python script that fetches a list of pages, finds every link pointing to your domain and reports its anchor text and rel values.
Install
pip install requests beautifulsoup4
The script
import csv
import sys
from urllib.parse import urljoin, urlparse
import requests
from bs4 import BeautifulSoup
TARGET = "example.com" # your domain, without www
def host(url):
h = urlparse(url).hostname or ""
return h.lower().removeprefix("www.")
def links_to_target(page_url):
r = requests.get(page_url, timeout=15, headers={"User-Agent": "Mozilla/5.0 (link-check)"})
soup = BeautifulSoup(r.text, "html.parser")
robots = soup.find("meta", attrs={"name": "robots"})
page_robots = robots["content"].lower() if robots and robots.get("content") else ""
found = []
for a in soup.find_all("a", href=True):
href = urljoin(page_url, a["href"])
if host(href) == TARGET or host(href).endswith("." + TARGET):
rel = " ".join(a.get("rel", [])) or "(none)"
found.append((href, a.get_text(strip=True)[:80], rel))
return r.status_code, page_robots, found
writer = csv.writer(sys.stdout)
writer.writerow(["page", "status", "page_robots", "link", "anchor", "rel"])
for line in open("pages.txt"):
page = line.strip()
if not page:
continue
try:
status, robots, found = links_to_target(page)
except requests.RequestException as e:
writer.writerow([page, "error", "", "", str(e)[:60], ""])
continue
if not found:
writer.writerow([page, status, robots, "", "NOT FOUND", ""])
for href, anchor, rel in found:
writer.writerow([page, status, robots, href, anchor, rel])
BeautifulSoup returns rel as a list, which is why the script joins it.
Reading the results
rel value |
Meaning |
|---|---|
(none) |
A normal followed link |
nofollow |
Asks search engines not to pass ranking signals |
ugc |
User-generated content, such as comments or forum posts |
sponsored |
Paid or affiliate placement |
noopener noreferrer |
Security and privacy hints only; they do not change how the link is followed |
Also check the page_robots column. A page with noindex in its meta robots tag will not appear in search results, so even a followed link on it is worth less.
Limits to know about
- Pages that build their content with JavaScript may show
NOT FOUNDeven though the link is visible in a browser. Check those manually. - Some sites block scripted requests. A 403 or 429 status means you should look in a browser instead.
- Links can be added or removed at any time, so re-run the check every few weeks.
Where this fits
Link status is one part of vetting. Before you pursue a placement at all, it helps to compare the referring sites themselves, for example with a free bulk DA PA checker to see authority and spam score for many domains side by side. Then, once links are live, this script tells you which ones actually exist and how they are tagged.
Top comments (0)