wiki-fetch
Wikipedia parser for Python and the CLI. Dependency-free.
own HTTP client, own HTML parser
Installation
uv add wiki-fetch
pip install wiki-fetch
Usage
CLI
| Option | Flag | Long | Default | Example |
|---|---|---|---|---|
| Article URL | -u |
--url |
None | https://en.wikipedia.org/wiki/The_Doors |
| Search query | -q |
--query |
None | The Doors (band) |
| Language edition | -l |
--lang |
English | English |
| Article part | -p |
--part |
all | infobox |
| Blocks to keep | -i |
--item |
all | first |
| Output format | -o |
--output |
text | text |
Give --url or --query; --url wins if both are set. A query takes the first search
result. A disambiguation page comes back whole, its lists of links included.
wiki-fetch -u https://en.wikipedia.org/wiki/The_Doors -p infobox -i first
output
Infobox:
The Doors:
The Doors:
Image: https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/The_Doors_1968.JPG/250px-The_Doors_1968.JPG
Caption: The Doors in 1966. From left to right: Jim Morrison, John Densmore, Ray Manzarek and Robby Krieger
Background information:
Origin: Los Angeles, California, U.S.
Genres:
Psychedelic rock
blues rock
acid rock
Years active:
1965-1973
1978
1993
1997
2000
2011-2012
2012-2013
2025
Labels:
Elektra
Rhino
Spinoffs:
The Psychedelic Rangers
Butts Band
Nite City
Manzarek-Krieger
Spinoff of: Rick & the Ravens
Past members:
Jim Morrison
Ray Manzarek
Robby Krieger
John Densmore
Website: thedoors.com
Image: https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/The_Doors_Logo.png/250px-The_Doors_Logo.png
URL: https://en.wikipedia.org/wiki/The_Doors
Exit codes: 0 printed a result, 1 fetch failed or the page holds no article, 2 bad input.
Python
| Argument | Values | Description |
|---|---|---|
| url | str |
Article URL |
| query | str |
Search query |
| lang | str |
Language name, endonym or code |
| part | infobox, paragraph, table, list, thumb, toc, all |
Article part |
| item | first, last, all |
Blocks to keep |
from wiki_fetch import Wiki
page = 'https://en.wikipedia.org/wiki/The_Doors'
output = Wiki(lang='English').search(url=page, part='infobox', item='first')
print(output.json)
search returns one object holding the result four ways: output.dict, output.json,
output.text, output.toml. item="first" and item="last" yield one dictionary per part,
item="all" a list of them.
output
{
"Infobox": {
"The Doors": {
"The Doors": {
"Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/The_Doors_1968.JPG/250px-The_Doors_1968.JPG",
"Caption": "The Doors in 1966. From left to right: Jim Morrison, John Densmore, Ray Manzarek and Robby Krieger"
},
"Background information": {
"Origin": "Los Angeles, California, U.S.",
"Genres": [
"Psychedelic rock",
"blues rock",
"acid rock"
],
"Years active": [
"1965-1973",
"1978",
"1993",
"1997",
"2000",
"2011-2012",
"2012-2013",
"2025"
],
"Labels": [
"Elektra",
"Rhino"
],
"Spinoffs": [
"The Psychedelic Rangers",
"Butts Band",
"Nite City",
"Manzarek-Krieger"
],
"Spinoff of": "Rick & the Ravens",
"Past members": [
"Jim Morrison",
"Ray Manzarek",
"Robby Krieger",
"John Densmore"
],
"Website": "thedoors.com",
"Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/The_Doors_Logo.png/250px-The_Doors_Logo.png"
}
}
},
"URL": "https://en.wikipedia.org/wiki/The_Doors"
}
toml
URL = "https://en.wikipedia.org/wiki/The_Doors"
[Infobox."The Doors"."The Doors"]
Image = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/The_Doors_1968.JPG/250px-The_Doors_1968.JPG"
Caption = "The Doors in 1966. From left to right: Jim Morrison, John Densmore, Ray Manzarek and Robby Krieger"
[Infobox."The Doors"."Background information"]
Origin = "Los Angeles, California, U.S."
Genres = ["Psychedelic rock", "blues rock", "acid rock"]
"Years active" = ["1965-1973", "1978", "1993", "1997", "2000", "2011-2012", "2012-2013", "2025"]
Labels = ["Elektra", "Rhino"]
Spinoffs = ["The Psychedelic Rangers", "Butts Band", "Nite City", "Manzarek-Krieger"]
"Spinoff of" = "Rick & the Ravens"
"Past members" = ["Jim Morrison", "Ray Manzarek", "Robby Krieger", "John Densmore"]
Website = "thedoors.com"
Image = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/The_Doors_Logo.png/250px-The_Doors_Logo.png"
Every error descends from WikiError, so one except WikiError catches the lot.
| Error | Raised when |
|---|---|
InputError |
an argument is rejected |
FetchError |
the transport fails |
StatusError |
Wikipedia answers 4xx or 5xx |
DecodeError |
the body does not decode |
PageError |
the page holds no article |
SelectorError |
the selector is outside the CSS subset |
StatusError and DecodeError sit under FetchError.
Specification
| Parts of page | Output formats |
|---|---|
infobox |
text |
paragraph |
json |
table |
dict |
list |
toml |
thumb |
|
toc |
|
all |
The parser normalises image addresses: protocol-relative links become https, Wikipedia's
utm_* tracking parameters go, every other parameter stays. toml writes bare keys where
TOML allows them and spells a missing value the way text does, as null.
Languages
--lang takes an English name, an endonym or a code, ignoring case and surrounding space:
German, Deutsch and de are the same edition. Thirty editions are known by name, most by
endonym too. The rest answer to their subdomain code, ISO 639-1 where one exists and 639-3
otherwise, with an optional variant suffix: fi, ceb, zh-yue. Anything else raises
InputError.
Selectors
select and select_one in wiki_fetch.base.html.query read this subset of CSS:
| Pattern | Example |
|---|---|
| type | table |
| id | #content |
| class | .infobox |
| attribute | [colspan] |
| attribute value | [typeof="mw:File"] |
| attribute prefix | [typeof^="mw:File"] |
| descendant | table .infobox-label |
| child | div > figure |
| grouping | .noprint, .navbox |
Classes match by token, so .box finds class="box wide" and skips class="boxed".
Anything outside the table raises SelectorError, pseudo-classes and sibling combinators
included.
License
Apache 2.0. See LICENSE.md.
Metadata
Release files for wiki-fetch 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wiki_fetch-1.0.0.tar.gz | 274.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wiki_fetch-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 313.8 kB
Release files / wiki_fetch-1.0.0.tar.gz
| Download URL | wiki_fetch-1.0.0.tar.gz |
|---|---|
| Size | 274.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d809339a761f73aa5f88346f057b3807ead58b8d5d21164b1736dbfe88d08945
|
|
BLAKE2b-256 checksum How to use checksums |
3628f70edfc49d4f8f3d7fd02de007051c06f4187947728495b73cfd68b24c85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / wiki_fetch-1.0.0-py3-none-any.whl
| Download URL | wiki_fetch-1.0.0-py3-none-any.whl |
|---|---|
| Size | 39.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f02dc5aff5a035dfa50ca0976cd4665351ded706fd428c5d3624edb5d077c02a
|
|
BLAKE2b-256 checksum How to use checksums |
7648a8885387069bef17ff83f2183b18c687de272940eaa32ffc17405f6b829a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|