- Blog
- Web Development
Turning A Repetitive Check Into A Small Tool
When I look at a new client’s site, the first thing I do is not open an expensive tool but run a small script. In five minutes I see the most basic problems: pages without a title, duplicate descriptions, images whose alt text was forgotten.
The script below does that. It does not replace a full SEO tool, but it tells you where to start.
Setup
Python 3.10 or later is enough. Two libraries are needed:
pip install requests beautifulsoup4
The Script
Save it as audit.py:
import sys
import requests
from bs4 import BeautifulSoup
HEADERS = {"User-Agent": "seo-pre-audit/1.0"}
def denetle(url: str) -> dict:
yanit = requests.get(url, headers=HEADERS, timeout=15)
sayfa = BeautifulSoup(yanit.text, "html.parser")
baslik = sayfa.title.get_text(strip=True) if sayfa.title else ""
aciklama_etiketi = sayfa.find("meta", attrs={"name": "description"})
aciklama = aciklama_etiketi.get("content", "").strip() if aciklama_etiketi else ""
canonical = sayfa.find("link", rel="canonical")
gorseller = sayfa.find_all("img")
altsiz = [g for g in gorseller if not g.get("alt", "").strip()]
sorunlar = []
if yanit.status_code != 200:
sorunlar.append(f"status code {yanit.status_code}")
if not baslik:
sorunlar.append("no title")
elif len(baslik) > 60:
sorunlar.append(f"long title ({len(baslik)} characters)")
if not aciklama:
sorunlar.append("no description")
elif len(aciklama) > 160:
sorunlar.append(f"long description ({len(aciklama)} characters)")
h1_sayisi = len(sayfa.find_all("h1"))
if h1_sayisi != 1:
sorunlar.append(f"{h1_sayisi} H1 tags")
if not canonical:
sorunlar.append("no canonical")
if altsiz:
sorunlar.append(f"{len(altsiz)}/{len(gorseller)} images have no alt text")
return {"url": url, "baslik": baslik, "sorunlar": sorunlar}
if __name__ == "__main__":
adresler = [satir.strip() for satir in open(sys.argv[1], encoding="utf-8") if satir.strip()]
for adres in adresler:
sonuc = denetle(adres)
durum = "OK" if not sonuc["sorunlar"] else "; ".join(sonuc["sorunlar"])
print(f"{sonuc['url']}\n {durum}\n")
Usage
Write the addresses you want to check into the urls.txt file, one per line:
https://ornek.com/
https://ornek.com/hizmetler/
https://ornek.com/iletisim/
Then run it:
python audit.py urls.txt
The output looks something like this:
https://ornek.com/
OK
https://ornek.com/hizmetler/
no description; 2 H1 tags; 7/12 images have no alt text
What Does The Script Look At?
- Status code. Anything other than 200 (a redirect chain, 404, 500) should be examined.
- Title. The blue line shown in the search result. What comes after about 60 characters is cut off.
- Description. It affects the click-through rate. It is cut off at around 160 characters; if you leave it empty, the search engine picks a random piece of the page.
- H1. The main heading of the page. Having none at all and having more than one both usually point to a template error.
- Canonical. Where the same content opens at different addresses, it says which one is the original.
- Alt text. Needed both for visually impaired visitors and for image search.
An Automatic List From The Sitemap
If you do not want to type the addresses by hand, you can pull them from the sitemap:
import requests
from xml.etree import ElementTree
def haritadan_adresler(harita_url: str) -> list[str]:
xml = requests.get(harita_url, timeout=15).content
kok = ElementTree.fromstring(xml)
ad_alani = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
return [loc.text for loc in kok.findall(".//s:loc", ad_alani)]
Its Limits
This script looks at the page’s HTML; it does not see content loaded afterwards with JavaScript. Topics such as speed, mobile use, internal link structure and content quality are also outside its scope. Still, on most small business sites you will see eight of the first ten things to fix here.
One note: when you crawl someone else’s site, send the requests sparsely and follow the site’s robots.txt rules.