Wikipedia API in Python: search, get a page, and find the right paragraph

Updated 2026-11-29 · 6 min read

To use the Wikipedia API from Python, send a GET to https://en.wikipedia.org/w/api.php with requests: list=search finds pages, and prop=extracts&explaintext=1 gives a page as plain text. Always send a real User-Agent. The hard part is the last step: a page is thousands of words, and you still have to find the one paragraph that answers your question.

Everything below was run on 29 November 2026 with Python and requests. The outputs are copied as they came back. Our test question: “when did the eiffel tower open”.

How do I search Wikipedia from Python?

Use the Action API's search. Set a User-Agent with your app's name and a way to reach you, or Wikipedia answers 403 Forbidden (see Wikipedia API rate limits).

import requests

API = "https://en.wikipedia.org/w/api.php"
s = requests.Session()
s.headers["User-Agent"] = "MyApp/1.0 (https://example.com; me@example.com) python-requests"

r = s.get(API, params={
    "action": "query", "list": "search",
    "srsearch": "when did the eiffel tower open",
    "srlimit": 3, "format": "json", "formatversion": 2,
})
for hit in r.json()["query"]["search"]:
    print(hit["title"])
Eiffel Tower
Gustave Eiffel
Victor Lustig

You get page titles and short snippets. The snippets are HTML with <span class="searchmatch"> tags around the matched words, so strip those before you show them. Good news: the right page came first.

How do I get the text of a Wikipedia page in Python?

Ask for the page's extract as plain text. It drops the tables, infobox and references and keeps the prose:

r = s.get(API, params={
    "action": "query", "prop": "extracts", "explaintext": 1,
    "titles": "Eiffel Tower", "format": "json", "formatversion": 2,
})
text = r.json()["query"]["pages"][0]["extract"]
paras = [p for p in text.split("\n") if p.strip() and not p.startswith("=")]
print(len(r.content), "bytes,", len(text.split()), "words,", len(paras), "paragraphs")
45608 bytes, 7592 words, 107 paragraphs

Lines that start with = are section headings (== History ==). Everything else is a paragraph. For one Wikipedia question, you now hold 7,592 words.

How do I find the paragraph that answers my question?

This is where most scripts stop being simple. The quick idea is to pick the paragraph that shares the most words with the question:

words = {"eiffel", "tower", "open"}
best = max(paras, key=lambda p: sum(w in p.lower() for w in words))
print(best[:200])
Little progress was made until 1886, when Jules Grévy was re-elected as president
of France and Édouard Lockroy was appointed as minister for trade. A budget for the
exposition was passed and, on 1 May, Lockroy announced an alteration to the terms of
the open competition ...

Wrong paragraph. It matched “open” in “open competition”. The answer sits in the section Inauguration and the 1889 exposition, in a paragraph that says “opened to the public” and “31 March”. Word counting can't tell “open competition” from “opened to the public”. To do better you need to cut pages into passages, turn them into embeddings (number lists that capture meaning), store them in a vector database, and keep all of it in step with Wikipedia's edits. That is a real project.

Should I use the wikipedia or Wikipedia-API package?

Both wrap the same API calls as above, so they save typing, not work. We checked PyPI today:

PackageLatest versionReleased
wikipedia1.4.015 Nov 2014
Wikipedia-API0.16.04 Sep 2026

wikipedia still works (it found the same 3 pages and the same 7,592 words), but it has had no release in 12 years. Wikipedia-API is kept up to date. Neither one picks the paragraph for you.

The same job with WikiVectors, in Python

One POST with your question. We keep English Wikipedia already cut into paragraphs, with meaning search and word search, updated within the hour of each edit:

import os, requests

r = requests.post(
    "https://wikivectors.com/v1/search",
    headers={"Authorization": "Bearer " + os.environ["WIKIVECTORS_KEY"]},
    json={"query": "when did the eiffel tower open", "limit": 3},
)
for x in r.json()["results"]:
    print(x["title"], "|", x["section"])
    print("  ", x["text"][:160])
Tourism in Paris | The Eiffel Tower (La Tour Eiffel)
   The Eiffel Tower is acknowledged as the universal symbol of both Paris and France. ...
   The Eiffel Tower was finally inaugurated on March 31, 1889. ...
Eiffel Tower | Inauguration and the 1889 exposition
   There was still work to be done, particularly on the lifts and facilities, and the
   tower was not opened to the public until nine days after the opening of the
   exposition on 6 May ...
Eiffel Tower | Inauguration and the 1889 exposition
   The main structural work was completed at the end of March 1889 and, on 31 March,
   Eiffel celebrated by leading a group of government officials ...

All three are answer paragraphs: inaugurated 31 March 1889, opened to the public in May 1889. The whole reply was 3,864 bytes, against 1,653 + 45,608 for the two Wikipedia calls. Each result also carries what you need to cite it:

{
  "title": "Eiffel Tower",
  "section": "Inauguration and the 1889 exposition",
  "url": "https://en.wikipedia.org/wiki/Eiffel_Tower",
  "license": "CC BY-SA 4.0",
  "revised_at": "2026-11-27T18:53:37Z"
}

revised_at is when Wikipedia saved the version you got: two days before our test, so you can see the text is current. Field list: fields.

What if I know the page name already?

Then you don't need search. /v1/pages takes up to 20 titles in one POST and returns each page's lead paragraph, description and revision. Case doesn't matter (“Eiffel tower” finds “Eiffel Tower”), and pages like “Mercury” that list several meanings come back with "disambiguation": true:

r = requests.post(
    "https://wikivectors.com/v1/pages",
    headers={"Authorization": "Bearer " + os.environ["WIKIVECTORS_KEY"]},
    json={"titles": ["Eiffel Tower"]},
)
page = r.json()["pages"][0]
print(page["description"], "|", page["text"][:90])
Tower in Paris, France | The Eiffel Tower (/ˈaɪfəl/ ⓘ EYE-fəl; French: Tour Eiffel [tuʁ ɛfɛl] ⓘ) is a lattic

Using LangChain or LlamaIndex? There is a ready retriever on frameworks. Want a short answer with its sources instead of paragraphs? Use /v1/answer.

You still need Wikipedia's own API to edit, or for things we don't cover (images, other languages, talk pages). The code at the top of this page is the right start for those.

Wikipedia for your AI app, without the limits

WikiVectors searches all of English Wikipedia and returns short, cited paragraphs, fresh within the hour. 1,000 free searches a month, then $2 per 1,000.

Just your email, no card. We send your key right away.

More guides