DEV Community

Ujjwal Dubey
Ujjwal Dubey

Posted on

Build a Call-to-Action Inventory: Extract Every Button and Link Label Across Your Site With Python

A homepage might say "Book a free call", the pricing page "Get started", the footer "Contact sales" and the blog "Talk to us". Four labels for one action. A free brand audit of your page copy will flag this on one page; this script shows it across the whole site.

import re, urllib.request
from html.parser import HTMLParser
from collections import Counter, defaultdict

class CTA(HTMLParser):
    def __init__(self):
        super().__init__(); self.stack = []; self.found = []
    def handle_starttag(self, tag, attrs):
        a = dict(attrs)
        if tag == "button" or (tag == "a" and re.search(r"btn|button|cta", a.get("class", ""))):
            self.stack.append([tag, a.get("href", ""), ""])
    def handle_data(self, d):
        if self.stack: self.stack[-1][2] += d
    def handle_endtag(self, tag):
        if self.stack and self.stack[-1][0] == tag:
            t, href, text = self.stack.pop()
            text = " ".join(text.split())
            if text: self.found.append((text, href))

def ctas(url):
    html = urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "cta-inventory"})).read().decode("utf-8", "ignore")
    p = CTA(); p.feed(html); return p.found

pages = ["https://example.com/", "https://example.com/services", "https://example.com/about"]
by_target = defaultdict(Counter)
for u in pages:
    for text, href in ctas(u):
        by_target[href or "(no href)"][text] += 1
for href, labels in by_target.items():
    flag = "  <- several labels" if len(labels) > 1 else ""
    print(href, dict(labels), flag)
Enter fullscreen mode Exit fullscreen mode

Reading the output

  • One target, several labels: pick one label and use it everywhere.
  • One label, several targets: "Get started" that sometimes opens a form and sometimes a pricing page confuses people.
  • Vague labels: "Learn more" and "Click here" tell the user nothing.

Limits

This reads server HTML only. If your buttons render with JavaScript, extract the rendered DOM first. Class-name matching is a heuristic; adjust the regex to your theme.

Combine the inventory with a tone check per page. The blunt single-page roast in the War Room gives a quick blunt read of a single page if you want a second opinion.

Top comments (0)