How to Use Python for NLP and Semantic SEO
Python is the programming language semantic SEO uses to turn text into data: entities, triples, query clusters, gaps and link graphs that a strategist reads before deciding anything. Five libraries cover the work: Requests and Beautiful Soup fetch and parse pages, spaCy extracts entities and grammatical relations, scikit-learn vectorizes and clusters text, pandas holds the tables, and NetworkX models internal links. Each task below is a short script tested on Python 3.13: preparing page text, extracting entities with spaCy, extracting triples from sentences, clustering queries into a query network, finding entity gaps against ranking pages, and auditing internal links on the live holisticradar.com link graph. Python measures and sorts; it cannot choose a source context, place an attribute in the core or outer section, or decide what a page should claim. The scripts join into one workflow that produces the inputs of a topical map, and the questions at the end cover how much Python a semantic SEO needs.
What Python does in semantic SEO
Python does three jobs in semantic SEO: it extracts structure from text, it compares documents and queries numerically, and it repeats both across thousands of pages faster than a person can read ten. Semantic SEO depends on knowing which entities a subject contains, which attributes searchers ask about, and how pages connect. Each of those facts is spread across search results, competitor pages and a site’s own content, and reading them by hand does not scale past one cluster.
Natural language processing (NLP) is the field that gives a program access to meaning. A search engine runs NLP on every document it indexes: it splits text into sentences, labels entities, parses grammatical relations and represents passages as vectors. Python runs the same operations at a smaller scale, so a semantic SEO sees a page closer to the way a search engine sees it.
The output of every script on this page is an input to a decision, not the decision. A query cluster tells a strategist which queries share an intent; the topical map decides whether that intent earns a URL.
The Python libraries semantic SEO uses
Five libraries cover most semantic SEO tasks, and each owns one step of the pipeline from a fetched page to a decision table.
| Library | Job in semantic SEO | Typical output |
|---|---|---|
| Requests and Beautiful Soup | Fetch pages and strip templates to the main content | Clean text per URL |
| spaCy | Sentence splitting, named entity recognition, dependency parsing, lemmatization | Entities, noun phrases, triples |
| scikit-learn | TF-IDF vectors, cosine similarity, clustering | Similarity scores, query clusters |
| pandas | Tables of URLs, queries, entities and metrics | CSV files a strategist can filter |
| NetworkX | Directed graphs of internal links | Inlink counts, PageRank, orphan pages |
The versions behind every listing here are Python 3.13, spaCy 3.8 with the en_core_web_sm pipeline, scikit-learn 1.9, pandas 3.0 and NetworkX 3.7. Sentence-transformer embedding models are a common sixth library: they replace TF-IDF vectors with vectors that capture synonyms, at the cost of downloading a model.
How to prepare page text before analysis
Every analysis starts from clean main content, because navigation, footers and cookie banners repeat on every page and distort every count. A menu that says “Services” on 200 pages makes “services” the most frequent term of the site. Preparation removes template elements, joins the remaining text, and reduces each word to its lemma, the dictionary form that groups “maps” with “map” and “planned” with “plan”.
import spacy
from bs4 import BeautifulSoup
html = """<html><body><nav>Home | Services | Contact</nav>
<main><h1>Topical maps</h1><p>Topical maps were planned before the articles were written.</p></main>
<footer>Copyright 2026</footer></body></html>"""
soup = BeautifulSoup(html, "html.parser")
for tag in soup(["nav", "footer", "script", "style"]):
tag.decompose()
text = soup.get_text(" ", strip=True)
nlp = spacy.load("en_core_web_sm")
lemmas = [t.lemma_.lower() for t in nlp(text) if t.is_alpha and not t.is_stop]
print(text)
print(lemmas)
Topical maps Topical maps were planned before the articles were written.
['topical', 'map', 'topical', 'map', 'plan', 'article', 'write']The navigation and footer are gone, stop words such as “were” and “before” are dropped, and seven lemmas remain. On a live site, Requests fetches each URL before Beautiful Soup parses it, and the tags to remove differ per theme: a WordPress theme may wrap content in an article element, a Shopify theme in a main element. Checking ten cleaned pages by eye before running a batch catches a wrong selector early, when it costs one rerun instead of a wrong report.
Lemmas are the unit for counting, and the original text is the unit for entity extraction and parsing. Named entity recognition needs capital letters and full sentences, so the scripts below receive the cleaned text, not the lemma list.
How to extract entities from a page with spaCy
Named entity recognition (NER) labels the spans of a text that name people, organizations, places, dates and quantities. The en_core_web_sm pipeline labels 18 entity types learned from the OntoNotes corpus. The listing below runs it on three sentences about the launch of the Google Knowledge Graph.
import spacy
nlp = spacy.load("en_core_web_sm")
text = (
"Google launched the Knowledge Graph in May 2012. "
"Amit Singhal announced it on the Google blog. "
"The Knowledge Graph held 500 million objects at launch."
)
doc = nlp(text)
for ent in doc.ents:
print(ent.text, ent.label_)The output lists five entities:
Google ORG
the Knowledge Graph ORG
May 2012 DATE
Google ORG
500 million CARDINALThe output shows both the value and the limit of NER. The dates, quantities and the company are correct. Amit Singhal, the Google engineer who announced the Knowledge Graph, is missed, and the Knowledge Graph is labeled an organization. NER recognizes that a span is a name; it does not know what the name refers to. Connecting a span to one node in a knowledge base is entity linking, a separate step that needs a knowledge base such as Wikidata. The Google Cloud Natural Language API performs both steps and returns a salience score per entity, which estimates how central the entity is to the document. A larger spaCy pipeline, such as en_core_web_trf, usually reduces mislabels at the cost of speed.
Entity extraction is the first measurement of a page in semantic SEO because entities are what a search engine stores. A page about topical maps that never names a central entity, a source context or an attribute is, to an extractor, a page about something else. Running the same extraction on the ranking pages and on the target page turns that observation into a list a writer can act on.
How to extract triples from sentences
A triple is a subject, predicate and object statement, and dependency parsing recovers it from the grammar of a sentence. spaCy labels each word with its grammatical role: the root verb, its nominal subject (nsubj) and its direct object (dobj). The listing reads those three roles from each sentence.
import spacy
nlp = spacy.load("en_core_web_sm")
text = (
"Google launched the Knowledge Graph in May 2012. "
"The Knowledge Graph connects entities. "
"Semantic SEO requires a topical map."
)
def span(token):
return " ".join(t.text for t in token.subtree)
for sent in nlp(text).sents:
root = sent.root
subj = [t for t in root.children if t.dep_ in ("nsubj", "nsubjpass")]
obj = [t for t in root.children if t.dep_ in ("dobj", "attr")]
if subj and obj:
print((span(subj[0]), root.lemma_, span(obj[0])))
('Google', 'launch', 'the Knowledge Graph')
('The Knowledge Graph', 'connect', 'entities')
('Semantic SEO', 'require', 'a topical map')Each sentence produced exactly one triple because each sentence states exactly one fact with the subject first. Sentences that bury the subject in a clause, chain three facts, or replace the subject with “it” produce no triple or a wrong one. That result is the practical test behind semantic triples in writing: a sentence that the small spaCy pipeline turns into a triple is a sentence a search engine extracts without guessing.
How to cluster queries into a query network
Query clustering groups queries that share one intent, so one URL answers the group and no two URLs compete for it. The listing clusters 15 real queries that holisticradar.com received impressions for in Google Search Console. TF-IDF over character n-grams turns each query into a vector, and agglomerative clustering merges queries whose cosine distance stays under 0.7.
from sklearn.cluster import AgglomerativeClustering
from sklearn.feature_extraction.text import TfidfVectorizer
queries = [
"semantic keyword grouping", "semantic keyword clustering",
"semantic keyword groups", "semantic core keyword grouping",
"semantic seo python", "python for semantic seo",
"how to use python for nlp and semantic seo",
"semantic markup seo", "seo semantic markup", "semantic markup html",
"semantic seo audit", "semantic audit",
"what is semantic seo", "semantic seo meaning", "semantic seo what is it",
]
vectors = TfidfVectorizer(analyzer="char_wb", ngram_range=(3, 5)).fit_transform(queries)
model = AgglomerativeClustering(
n_clusters=None, metric="cosine", linkage="average", distance_threshold=0.7
)
labels = model.fit_predict(vectors.toarray())
for label in sorted(set(labels)):
print(label, [q for q, l in zip(queries, labels) if l == label])
0 ['semantic keyword grouping', 'semantic keyword clustering', 'semantic keyword groups', 'semantic core keyword grouping']
1 ['semantic seo python', 'python for semantic seo', 'how to use python for nlp and semantic seo']
2 ['semantic seo audit', 'semantic audit']
3 ['what is semantic seo', 'semantic seo what is it']
4 ['semantic markup seo', 'seo semantic markup', 'semantic markup html']
5 ['semantic seo meaning']Five clusters match five intents. The sixth cluster is an error: “semantic seo meaning” asks the same question as “what is semantic seo”, but the two share almost no characters. Character vectors measure spelling, not meaning. Embedding vectors or SERP overlap, the share of identical ranking URLs between two queries, fix that error. The threshold is a choice the strategist tests: at 0.6 the keyword cluster split in two, separating “semantic keyword clustering” from “grouping” although both ask for the same method. Clusters become rows of a query network only after a person confirms the intent of each.
How to find entity gaps against ranking pages
An entity gap is an entity that most ranking pages mention and the target page does not. The listing extracts named entities and noun phrases from three reference passages, keeps the terms at least two references share, and subtracts the terms of the target.
import spacy
from collections import Counter
nlp = spacy.load("en_core_web_sm")
def terms(text):
doc = nlp(text)
found = {e.text.lower() for e in doc.ents}
for chunk in doc.noun_chunks:
words = [t.lemma_.lower() for t in chunk if not t.is_stop and t.is_alpha]
if words:
found.add(" ".join(words))
return found
references = [
"A topical map lists the central entity, its attributes and the source context. "
"Each attribute gets one URL, and internal links follow the map.",
"The topical map starts from a central entity. Search intent and source context "
"decide which attributes enter the core section.",
"Koray Tuğberk Gübür defined the topical map around a central entity, "
"attributes, source context and contextual bridges.",
]
target = "A topical map is a list of keywords grouped into clusters with one pillar page."
counts = Counter(t for ref in references for t in terms(ref))
missing = sorted(t for t, n in counts.items() if n >= 2 and t not in terms(target))
print(missing)
['attribute', 'central entity', 'source context']The target describes a topic cluster and calls it a topical map, and the script finds the three terms that separate the two models. The threshold of two references filters out terms one author used alone. On real pages the references are the main content of the top ten results, and the list is longer and noisier. The entity gap article covers which gaps to close and which to ignore.
Two refinements make the list usable at scale. Weighting each term by the number of references that contain it ranks consensus terms above incidental ones. Comparing lemmas, not surface forms, stops “attributes” and “attribute” from counting as two terms. Both changes are one line each in pandas once the terms sit in a table with one row per reference and term.
How to audit internal links with NetworkX
An internal link audit models a site as a directed graph: every URL is a node and every contextual link is an edge. The listing below ran on links.csv, the 570 contextual links written in the body content of holisticradar.com on 21 November 2026, with navigation and footer links excluded.
import networkx as nx
import pandas as pd
links = pd.read_csv("links.csv")
graph = nx.from_pandas_edgelist(links, "source", "target", create_using=nx.DiGraph)
articles = [url for url in graph if url.startswith("/learn/") and url != "/learn/"]
inlinks = {url: graph.in_degree(url) for url in articles}
print("Articles:", len(articles), "Links:", graph.number_of_edges())
print("Fewest inlinks:", sorted(inlinks.items(), key=lambda x: x[1])[:3])
rank = nx.pagerank(graph)
top = sorted(articles, key=rank.get, reverse=True)[:5]
print("Highest PageRank:", top)
Articles: 47 Links: 570
Fewest inlinks: [('/learn/semantic-seo-examples/', 3), ('/learn/ymyl-seo/', 4), ('/learn/declared-order/', 4)]
Highest PageRank: ['/learn/authority-score/', '/learn/retrieval-cost/', '/learn/topical-map/', '/learn/query-network/', '/learn/internal-linking/']The graph shows where link equity collects and where it is thin. The five articles with the highest PageRank are method definitions that most other articles cite, which matches their role. The examples article had three contextual inlinks, the fewest of any article, so it received new links from related articles after this audit. PageRank on a graph of one site is a relative measure of internal prominence, not Google’s score. The rules for which page links to which belong to internal linking, and the script only reports where the graph breaks them.
What Python cannot decide in semantic SEO
Python cannot decide anything that depends on the business behind the site. Source context, the identity and monetization model of the publisher, decides which attributes a site covers and how deep it goes. No vector encodes it. Two agencies with the same query data build different maps because one sells audits and the other sells content production.
Python also cannot verify facts. A script that finds an entity gap reports that competitors mention an entity; it does not report whether their statements about it are true. Information gain, the facts a page adds beyond the ranking consensus, comes from data, experience and testing that no ranking page contains, so no script trained on ranking pages can produce it.
The last limit is placement. A cluster of queries can belong in the core section, in the outer section, or nowhere on the site. That choice follows the predicate pair of the site and the bridge each page must carry, and it is written into the topical map by a person.
A Python workflow for one topical map
The scripts join into one workflow that runs once per topical map and again when search results change. The order is fixed because each step consumes the output of the step before it.
- Export queries from Search Console and from a keyword source, and cluster them.
- Fetch the top ten results for one query per cluster and strip each page to its main content.
- Extract entities, noun phrases and triples from the references and from the site’s own page.
- Compute entity gaps and similarity scores per cluster.
- Build the internal link graph of the site and list pages with weak inlinks.
- Merge everything into one pandas table per cluster and export it as CSV.
Rerunning the workflow monthly turns it from a research tool into a monitor. A new entity that appears in most ranking pages signals that the consensus of a query has moved; a cluster that splits signals that searchers now separate two intents; a page whose inlinks drop signals that a redesign removed contextual links. Each change is visible only against the previous month’s table, which is why every run saves its output with the date in the file name.
The CSV goes to the strategist, who confirms intents, assigns each cluster to a URL, and writes the brief. The semantic analysis article covers how to read similarity scores and term gaps once the table exists.
Python for semantic SEO questions
Does semantic SEO require Python?
Semantic SEO does not require Python, and a strategist can build a topical map from search results by reading them. Python becomes necessary when a map covers more than a few hundred queries or when the same analysis repeats every month, because manual reading at that scale skips pages.
Which Python library is best for semantic SEO?
spaCy is the most useful single library for semantic SEO because entity extraction, lemmatization and dependency parsing are the three operations closest to how a search engine reads a page. scikit-learn is the second, for similarity and clustering.
Can ChatGPT replace Python scripts for semantic SEO?
A language model can label and summarize text, but its output changes between runs and it cannot count reliably across thousands of rows. Python returns the same output for the same input, which makes results comparable from one month to the next.
Python for Semantic SEO in the semantic SEO topical map
Python for Semantic SEO is the method attribute of the semantic analysis entity inside the Holistic Radar topical map, and it sits in the outer section under the Know predicate.
The outer section holds the page because it teaches a method without selling one, and its bridge passes session signals toward Query Network Research, where the same scripts run on a client’s queries.
→ Query network research: the clustering and gap scripts run on a client’s own queries.
→ What is semantic analysis: how to read similarity scores and term gaps.
→ What is an entity gap: which missing entities to add and which to ignore.
→ Semantic triples: the sentence form a parser turns into a fact.