Sub topic 16
Scaling the workflow with scripts and tools
This chapter assumes you can read basic Python, know what HTTP status codes such as 200, 301 and 404 mean, and have handled CSV exports from SEO tools. You have already done both tactics by hand. Now you will see where scripts save time and where they should stop.
What to automate and what to leave manual
Some steps are mechanical. Others need judgment. Automating the wrong ones produces bulk outreach, which is the spam this workflow is meant to avoid.
| Step | Automate? | Reason |
|---|---|---|
| Checking link status | Yes | Repetitive, with clear rules |
| Checking whether a mention has a link | Yes | A fixed test on the HTML |
| Collecting search results | Partly | Tools and alerts help, but results need review |
| Judging relevance of a replacement page | No | Needs human judgment |
| Writing and sending outreach | No | Needs a specific, personal message |
Scanning many pages for broken links
The broken link function from the earlier chapter checks one page. To scan a list of pages, loop over it and write the results to a CSV. This version also pauses between pages so you do not hammer any one site.
import csv
import time
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
def scan_pages(pages, out_path, delay=2.0):
with open(out_path, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["source_page", "broken_url", "status"])
for page in pages:
try:
for url, status in find_broken_links(page):
writer.writerow([page, url, status])
except requests.RequestException as exc:
writer.writerow([page, "", f"page error: {exc.__class__.__name__}"])
time.sleep(delay)
if __name__ == "__main__":
scan_pages(["https://example.com/resources"], "broken_candidates.csv")The time.sleep call is a politeness limit. Keep it, and respect each site's robots.txt rules.
Filtering mention candidates
Your search operator queries and alerts produce a list of URLs. A second script can fetch each one and keep only pages that mention the brand without linking to it. Use is_unlinked_mention from the verification chapter.
import requests
from mentions import is_unlinked_mention # the function from the verification chapter
HEADERS = {"User-Agent": "link-audit/1.0"}
def filter_mentions(urls, brand, domain):
keep = []
for url in urls:
try:
resp = requests.get(url, headers=HEADERS, timeout=10)
except requests.RequestException:
continue
if resp.ok and is_unlinked_mention(resp.text, brand, domain):
keep.append(url)
return keepSave the earlier function in mentions.py so the import works.
Common errors
- Trusting the script's output as final. A 404 can be a soft 404 or a temporary outage. Open each candidate in a browser before you contact anyone.
- Missing JavaScript-rendered links.
requestssees only the raw HTML. A link added by JavaScript is invisible to it, so a mention may look unlinked when it is not. - Scanning too fast. Rapid requests can get your IP blocked and slow the target site.
- Automated outreach. Mass-mailing a template ignores the qualification step and risks complaints.
Tools that do the same job
Crawlers and backlink tools can list broken outbound links and export them to CSV. Mention-monitoring tools and search alerts deliver new mentions as they appear. Scripts add value when you need a custom check, such as the brand-and-domain test above, or when you want to chain steps together. Use a tool when its report already answers your question.
Exercise
Your script flags 40 pages as "unlinked mentions", but a manual check shows 6 of them already link to your site through a button added by JavaScript. Why did this happen, and what are two ways to handle it?
Show answer
The requests library fetches only the raw HTML and does not run JavaScript, so links added by scripts are missing from what the function sees. You can either render the page with a headless browser tool before checking, or manually open the flagged pages and confirm the link is absent before you send outreach.