SEO

Python and AI for SEO: Automate Audits and Keyword Clusters

Most SEO work isn't hard, it's repetitive. Here's how a few small Python scripts and an AI model can take over the dull parts while you keep the final say.

Pipeline showing Python crawling pages, AI analysing keywords, drafting meta tags and a human reviewing the result

Check the title tag. Check the meta description. Check the images. Then do it all again for the next page, and the one after that. That's what a lot of SEO work really looks like.

I used to handle it in spreadsheets. It was fine for 20 pages. At 200 pages it swallowed entire weekends. That's when Python stopped feeling like a "developer thing" and started feeling like a basic tool for anyone who runs a website. This guide covers three jobs where Python and AI together save real time, plus the places where AI should not be left alone.

Why Python fits SEO work so well

SEO is mostly patterns in text and data, and Python is good at both. It can fetch a page, read the HTML, compare thousands of keywords and write the results to a CSV in a handful of lines.

AI adds the part a plain script can't do: judging meaning. A script can tell you a title is 80 characters long. A language model can tell you it's vague. The split is simple:

If you want the bigger picture first, read our guides on building an SEO system and an AI-powered workflow.

Setup in five minutes

You need Python 3.9 or newer. Create a virtual environment and install three packages:

python -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate
pip install requests beautifulsoup4 pandas

Later we'll add sentence-transformers and scikit-learn for keyword clustering.

Job 1: A quick on-page audit

Get the basics right before touching any AI. This script checks the things search engines read first: the title, the meta description, H1 headings and image alt text.

import requests
from bs4 import BeautifulSoup

def audit_page(url):
    resp = requests.get(url, timeout=15,
                        headers={"User-Agent": "my-seo-audit/1.0"})
    soup = BeautifulSoup(resp.text, "html.parser")

    title = soup.title.string.strip() if soup.title and soup.title.string else ""
    meta = soup.find("meta", attrs={"name": "description"})
    description = meta["content"].strip() if meta and meta.get("content") else ""
    h1s = [h.get_text(strip=True) for h in soup.find_all("h1")]
    images = soup.find_all("img")
    no_alt = [img.get("src") for img in images if not (img.get("alt") or "").strip()]

    return {
        "url": url,
        "status": resp.status_code,
        "title": title,
        "title_length": len(title),
        "description_length": len(description),
        "h1_count": len(h1s),
        "images": len(images),
        "images_missing_alt": len(no_alt),
    }

print(audit_page("https://example.com"))

Loop this over the URLs in your sitemap, drop the results into a pandas DataFrame and save it with df.to_csv("audit.csv"). You now have a site-wide health check you can rerun every month.

Look for titles that are blank, duplicated or too long, meta descriptions that are missing, pages with no H1 or more than one, and images without alt text. Only crawl your own site (or sites you have permission to crawl) and add a short pause between requests so you don't strain the server.

Job 2: Group keywords by meaning, not spelling

Say a research tool hands you 300 keywords. Sorting them by hand is slow, and sorting by shared words is misleading. "Python SEO script" and "automate audits with code" share no words, yet they belong together.

Embeddings solve this. They turn each phrase into a list of numbers that reflects its meaning, so similar phrases land close together. A clustering algorithm then groups them.

import pandas as pd
from sentence_transformers import SentenceTransformer
from sklearn.cluster import KMeans

keywords = [
    "python seo script", "automate seo audit", "seo automation with code",
    "ai meta description generator", "write meta tags with ai",
    "image alt text best practice", "alt text for seo", "how to write alt text",
    "keyword clustering python", "group keywords by intent",
]

model = SentenceTransformer("all-MiniLM-L6-v2")
vectors = model.encode(keywords)

kmeans = KMeans(n_clusters=3, n_init=10, random_state=42)
labels = kmeans.fit_predict(vectors)

df = pd.DataFrame({"keyword": keywords, "cluster": labels})
print(df.sort_values("cluster"))

Each cluster is a candidate for one article or one section of a page. If five keywords land together, one well-written page can target all of them, instead of five thin pages competing with each other.

Choose the number of clusters by trial. Make a guess, read the groups, then adjust. If two clusters feel like the same topic, lower the number. If one cluster is a messy mix, raise it. The model downloads on the first run; after that it works offline and costs nothing per query.

BITOMANCY PICK

Smart Copy PRO

Turn copied text into summaries, professional emails and LinkedIn-ready content in seconds.

Get Smart Copy PRO →

Job 3: Draft meta tags and alt text, then edit them

Writing 200 meta descriptions is nobody's idea of fun. An AI model can produce first drafts quickly if your instructions are clear. A prompt that works well looks like this:

Write a meta description for this page. Max 155 characters. Include the main keyword naturally. Describe what the reader will get. No hype words, no quotation marks. Page title: {title}. Page summary: {summary}.

Send that prompt through whichever AI provider you use, loop it over your pages in Python and save the results in a CSV column called draft_description.

Alt text works the same way with a vision-capable model. Give it the image and ask for one plain sentence describing what is shown and why it's on the page. Good alt text describes the image for someone who can't see it; it is not a place to stuff keywords.

My rule: AI drafts, I edit. Every generated line gets read before it goes live. Models sometimes invent details, repeat the same phrasing across pages or describe an image wrongly. A site full of near-identical descriptions looks lazy to readers and adds nothing for search.

Where AI should stay out of the driver's seat

A simple weekly routine

This loop fits into about an hour:

  1. Run the audit script and open the CSV.
  2. Fix the worst five problems: missing titles, missing alt text, duplicates.
  3. Cluster any new keywords and pick the cluster for your next post.
  4. Draft meta tags with AI, then edit each one by hand.
  5. Re-run the audit next week and compare.

Small, steady passes beat one giant cleanup you never repeat.

Image SEO checklist

Key Takeaways

Frequently Asked Questions

Do I need to be a programmer to use Python for SEO?

No. If you can run a script and read a CSV file, you can follow along. Start with the audit script and change one line at a time.

Is using AI for SEO against Google's rules?

Search engines focus on whether content is useful and original, not on the tool used to make it. Mass-produced, low-quality pages are the problem. Reviewed, original and helpful content is fine.

Which AI model should I use for SEO tasks?

Any capable language model works for first drafts. For keyword clustering, a free local embedding model such as the one above is enough.

How often should I audit my website?

Monthly is plenty for most sites. Run an audit straight away after any big change to templates, URLs or navigation.

Conclusion

Python and AI don't replace SEO judgement. They remove the dull parts so you have time for the work that matters: understanding what your readers want and writing something worth reading. Run the audit script this week, fix what it finds, and add clustering next time. In a month you'll have a repeatable system instead of a pile of spreadsheets.

Explore More on BITOMANCY

Want more practical AI and technology guides? Visit the BITOMANCY homepage for more articles on artificial intelligence, automation, SEO and emerging technology.