Skip to content

Best 10 Python Scripts for SEO Automation

Best 10 Python Scripts for SEO Automation
On this page 16
  1. Why Python is the SEO team's favorite tool
  2. The 10 scripts at a glance
  3. 1. Keyword rank tracking
  4. 2. Backlink analysis
  5. 3. Competitor analysis
  6. 4. Content optimization suggestions
  7. 5. Internal linking audit
  8. 6. XML sitemap generator
  9. 7. Image optimization
  10. 8. 404 error checker
  11. 9. SERP scraping
  12. 10. Log file analysis
  13. How the 10 scripts map to your workflow
  14. Build vs. outsource
  15. Time saved by automating
  16. Frequently asked questions

SEO professionals spend a lot of time optimizing content, tracking performance, and trying to stay ahead of competitors. But what if you could automate some of these tasks and focus more on strategy and creativity?

Python has become one of the most popular languages among SEO teams. It is easy to learn, powerful, and backed by libraries that handle almost any job. Python scripts can automate the repetitive, time-consuming parts of SEO, such as finding the right keywords, checking the health of a site, and analyzing backlinks. That leaves marketers free to spend more time on strategy and creative work.

Below are 10 Python scripts that make SEO tasks easier. Whether you are an experienced specialist or just starting out, they will help you save time, improve accuracy, and lift your site's performance in search. If coding is not your thing, you can also hire an SEO specialist to set them up for you.

For the wider toolkit beyond these scripts, see our roundup of the best Python libraries and tools for SEO and AEO and the best AI SEO tools for visualization and reporting.

Why Python is the SEO team's favorite tool

Python is readable, and its ecosystem is huge. According to the Stack Overflow Developer Survey, Python is one of the most used and most wanted languages year after year, which means help and tutorials are everywhere. For SEO, three libraries do most of the heavy lifting.

requests9BeautifulSoup8pandas7matplotlib3selenium2
Core libraries by how often SEO scripts use them

The pattern is simple. requests fetches pages or calls APIs, BeautifulSoup parses the HTML, and pandas turns the messy result into a clean table or CSV. Learn those three and you can adapt most of the scripts below.

The 10 scripts at a glance

  1. 1
    Track rankingsLog keyword positions to a CSV over time
  2. 2
    Audit linksPull and score your backlink profile
  3. 3
    Study rivalsBenchmark competitors on keywords and links
  4. 4
    Fix contentScore on-page copy and suggest edits
  5. 5
    Map internal linksFind orphan pages and weak structure
  6. 6
    Ship a sitemapAuto-generate a fresh XML sitemap
  7. 7
    Shrink imagesFlag heavy files and missing alt text
  8. 8
    Catch 404sReport every broken link
  9. 9
    Read the SERPScrape titles and URLs that rank
  10. 10
    Mine the logsSee how Googlebot really crawls you
From setup to insight

1. Keyword rank tracking

Why it matters

Tracking keyword rankings is vital for understanding how well your content performs in search. It lets you monitor SEO campaigns and make adjustments before rankings slip. Our guide on how to check Google keyword ranking with Python walks through the same idea in more depth.

How it works

This script tracks the rankings of chosen keywords across search engines like Google and Bing. Results are stored in a CSV file or database for easy analysis over time.

Key libraries: BeautifulSoup for parsing HTML and extracting ranking data, requests for HTTP requests, and pandas for handling the data.

import requests
from bs4 import BeautifulSoup
import pandas as pd

keywords = ["python automation", "SEO automation"]
search_url = "https://www.google.com/search?q={}"

def get_rank(keyword):
    response = requests.get(search_url.format(keyword))
    soup = BeautifulSoup(response.text, 'html.parser')
    rank = soup.find('div', {'class': 'BNeawe'}).text
    return rank

results = {keyword: get_rank(keyword) for keyword in keywords}
df = pd.DataFrame.from_dict(results, orient='index', columns=['Rank'])
df.to_csv('keyword_ranks.csv')

Use it to track how your site ranks for important keywords, spot trends, and act early to optimize content.

Why it matters

Backlinks are a major factor in a site's authority and ranking. Google's own Search Central documentation treats links as a core signal, so regular analysis of your profile helps you spot toxic links and understand anchor text distribution.

How it works

This script pulls backlink data from tools like Ahrefs or SEMrush and analyzes it for domain authority, anchor text, and link type. Key libraries are BeautifulSoup for scraping, requests for API calls, and pandas for data manipulation.

import requests
import pandas as pd

api_url = "https://api.ahrefs.com/v1/backlinks?target=yourdomain.com&output=json&token=yourtoken"

response = requests.get(api_url)
backlinks = response.json()['backlinks']

df = pd.DataFrame(backlinks)
df.to_csv('backlink_analysis.csv')

Use it to disavow harmful links, see how competitors earn their links, and refine your own strategy.

3. Competitor analysis

Why it matters

Understanding your competitors' SEO strategies shows you what works and what does not, so you can adjust tactics to outperform them.

How it works

This script gathers data from competitor sites, including target keywords, backlinks, and content structure, then compares it with your own performance. It uses BeautifulSoup to extract data, pandas to compare it, and matplotlib to visualize the result.

import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt

competitors = ["competitor1.com", "competitor2.com"]
keyword = "SEO automation"

def get_competitor_data(domain):
    response = requests.get(f"https://{domain}/search?q={keyword}")
    soup = BeautifulSoup(response.text, 'html.parser')
    backlinks = soup.find_all('a')
    return len(backlinks)

data = {comp: get_competitor_data(comp) for comp in competitors}
df = pd.DataFrame.from_dict(data, orient='index', columns=['Backlinks'])
df.plot(kind='bar')
plt.show()

Use it to benchmark your performance, find gaps, and discover new opportunities for growth.

4. Content optimization suggestions

Why it matters

On-page optimization is key to ranking well. Content that targets specific keywords clearly can lift your visibility. For teams that want a human touch, a content manager can turn the script's output into publish-ready copy.

How it works

This script analyzes content for keyword density, LSI (Latent Semantic Indexing) keywords, meta tags, and readability, then suggests improvements. It uses BeautifulSoup to extract content, nltk for language processing, and spacy for more advanced analysis.

import nltk
from bs4 import BeautifulSoup
import requests

nltk.download('punkt')

url = "https://yourwebsite.com/your-page"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

text = soup.get_text()
tokens = nltk.word_tokenize(text)
fdist = nltk.FreqDist(tokens)

print(fdist.most_common(10))  # Print top 10 keywords

Use it to make on-page content keyword-rich and aligned with your SEO goals.

5. Internal linking audit

Why it matters

A strong internal linking structure spreads page authority across your site and makes it easier for search engines to crawl and index your content.

How it works

This script crawls your site, maps the internal linking structure, and finds orphan pages that no internal links point to. It uses scrapy for crawling and networkx to visualize the link graph.

import scrapy
import networkx as nx

class InternalLinkSpider(scrapy.Spider):
    name = "internallinks"
    start_urls = ['https://yourwebsite.com']

    def parse(self, response):
        for link in response.css('a::attr(href)').getall():
            yield response.follow(link, self.parse)

G = nx.DiGraph()
# Add nodes and edges to G as you crawl
nx.draw(G, with_labels=True)

Audit internal links regularly to keep a logical, efficient structure that helps both SEO and users.

6. XML sitemap generator

Why it matters

An XML sitemap helps search engines understand your site's structure and makes sure all important pages get crawled and indexed.

How it works

This script generates an up-to-date XML sitemap by crawling your site and collecting every relevant URL. It uses lxml to build the XML, os for file operations, and datetime for timestamps.

import datetime
from lxml import etree

urlset = etree.Element('urlset', xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")

urls = ["https://yourwebsite.com/page1", "https://yourwebsite.com/page2"]

for url in urls:
    url_elem = etree.SubElement(urlset, "url")
    loc = etree.SubElement(url_elem, "loc")
    loc.text = url
    lastmod = etree.SubElement(url_elem, "lastmod")
    lastmod.text = datetime.datetime.now().strftime("%Y-%m-%d")

tree = etree.ElementTree(urlset)
tree.write("sitemap.xml", pretty_print=True, xml_declaration=True, encoding="UTF-8")

Use it to keep your sitemap fresh automatically so crawlers always see your latest pages.

7. Image optimization

Why it matters

Optimizing images cuts page load times, which affects both user experience and rankings. Google's Core Web Vitals make load speed a direct ranking consideration.

How it works

This script scans your site for images, checks for missing alt tags, flags large files, and suggests fixes. It uses PIL (Pillow) for image processing, os for files, and requests for downloads.

from PIL import Image
import os

image_folder = "/path/to/images"

for filename in os.listdir(image_folder):
    with Image.open(os.path.join(image_folder, filename)) as img:
        print(f"{filename} - Size: {img.size} - Format: {img.format}")
        if img.size > (1000, 1000):  # Example condition for large images
            img.thumbnail((1000, 1000))
            img.save(os.path.join(image_folder, "optimized", filename))

Run it periodically so every image stays optimized, leading to faster loads and better SEO.

8. 404 error checker

Why it matters

Broken links hurt user experience and can drag down rankings if left unfixed.

How it works

This script crawls your site, checks for broken links, and reports every 404 it finds. It uses requests to check status codes, BeautifulSoup to extract links, and pandas for the report.

import requests

def check_link(url):
    response = requests.get(url)
    if response.status_code == 404:
        return False
    return True

urls = ["https://yourwebsite.com/page1", "https://yourwebsite.com/page2"]
broken_links = [url for url in urls if not check_link(url)]

print("Broken Links:", broken_links)

Run it regularly to catch and fix broken links and keep your site's SEO health strong.

9. SERP scraping

Why it matters

Scraping search engine result pages lets you gather competitive data, analyze keyword trends, and see how different sites rank.

How it works

This script scrapes SERPs for specific keywords and collects titles, meta descriptions, and URLs of the top pages. It uses BeautifulSoup to parse HTML and selenium to automate browser actions when needed.

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome()
driver.get("https://www.google.com/search?q=python+SEO+automation")

soup = BeautifulSoup(driver.page_source, 'html.parser')
results = soup.find_all('h3')

for result in results:
    print(result.text)

driver.quit()

Use it to watch keyword competition, track your pages, and study SERP features like featured snippets.

10. Log file analysis

Why it matters

Server log files show exactly how search engines crawl your site. Analyzing them helps you find issues, optimize crawl budget, and understand crawler behavior.

How it works

This script parses server logs to spot crawl errors, crawl frequency, and other patterns that affect SEO. It uses pandas to analyze the data, re for pattern matching, and matplotlib to visualize crawls.

import pandas as pd
import re

log_file = "/path/to/logfile.log"
logs = []

with open(log_file, "r") as file:
    for line in file:
        if "Googlebot" in line:
            logs.append(line)

df = pd.DataFrame(logs, columns=["Log Entry"])
df['Date'] = df['Log Entry'].apply(lambda x: re.search(r'\d{2}/\w{3}/\d{4}', x).group())
df['URL'] = df['Log Entry'].apply(lambda x: re.search(r'GET\s(.*)\sHTTP', x).group(1))

df.to_csv('googlebot_crawls.csv')

Use it to understand crawler behavior, optimize crawl budget, and detect issues holding back your SEO.

How the 10 scripts map to your workflow

Not every script fits every stage of SEO work. This table groups them by the job they do best.

ScriptMain jobCore librariesHow often to run
Keyword rank trackingMonitor positionsrequests, BeautifulSoup, pandasWeekly
Backlink analysisAudit link profilerequests, pandasMonthly
Competitor analysisBenchmark rivalsBeautifulSoup, pandas, matplotlibMonthly
Content optimizationImprove on-page copynltk, spacy, BeautifulSoupPer page
Internal linking auditFix site structurescrapy, networkxQuarterly
XML sitemap generatorHelp crawlerslxml, os, datetimeOn publish
Image optimizationSpeed up pagesPillow, os, requestsMonthly
404 error checkerFind broken linksrequests, BeautifulSoupWeekly
SERP scrapingStudy rankingsBeautifulSoup, seleniumAs needed
Log file analysisRead crawl datapandas, re, matplotlibMonthly

Build vs. outsource

Writing and maintaining these scripts takes time. Some teams prefer to run them in-house, others hand the work to specialists. Here is a quick comparison to help you decide.

In-house
  • Full control over the code
  • Requires Python skills
  • Ongoing maintenance on you
  • Higher time cost
vs
Outsourced
  • Faster to get started
  • Handled by vetted experts
  • Maintenance included
  • 50 to 70 percent lower cost via Asia talent
Running scripts in-house vs. outsourcing

If the second column sounds better, our SEO outsourcing services and website managers can run and maintain these scripts for you. See how it works and pricing for details, or read real case studies first.

Time saved by automating

The real payoff of automation is hours returned to your week. These figures are typical estimates for a mid-size site, not exact numbers.

8-12 hrs
Rank tracking and reporting
6-10 hrs
Backlink and competitor audits
5-8 hrs
Broken link and crawl checks
20+ hrs
Combined across all 10 scripts
Rough time saved per month

Frequently asked questions

Do I need to be a programmer to use these Python SEO scripts?

No. Basic Python knowledge helps, but you can copy, adapt, and run most of these scripts with a little practice. Start with the simpler ones like the 404 checker or sitemap generator, then move to scraping and log analysis. If you would rather skip the learning curve, you can hire an SEO specialist to set everything up.

Which Python libraries should I learn first for SEO?

Start with three: requests to fetch pages and call APIs, BeautifulSoup to parse HTML, and pandas to organize data into tables and CSVs. Almost every script in this list uses that trio. Add matplotlib for charts and selenium for browser automation once you are comfortable.

Scraping public data is generally allowed, but always respect a site's robots.txt, terms of service, and rate limits. Use official APIs like Ahrefs or SEMrush where possible, add delays between requests, and never overload a server. When in doubt, review Google Search Central guidance or ask an experienced team to handle it responsibly.

Mojahar Ali
Written by

Principal SEO Consultant

Mojahar Ali is Seotal's co-founder and SEO/GEO lead, with over 7 years in search. He has grown organic pipelines from zero to 100+ monthly qualified leads in HR tech, and specializes in technical SEO, generative engine optimization (LLM and answer-engine visibility), web analytics, and marketing automation.

Technical SEOGenerative Engine OptimizationLLM OptimizationWeb AnalyticsMarketing Automation

More reads

Back to the blog