wikiquote
The wikiquote
package for Python (>=3.8) allows you to search and retrieve quotes from any Wikiquote article, as well as retrieve the quote of the day.
Installation
You can install the wikiquote
package using pip
:
$ pip install --upgrade wikiquote
Usage
>>> import wikiquote
>>> wikiquote.search('The Matrix')
>>> wikiquote.quotes('The Matrix (film)', max_quotes=2)
>>> wikiquote.quote_of_the_day()
>>> wikiquote.qotd()
>>> wikiquote.random_titles(max_titles=3)
>>> wikiquote.supported_languages()
Some article titles will lead to a Disambiguation page (like Matrix
), which will raise a DisambiguationPageException
exception. Usually this happens because there are many articles matching the search term. When this happens, try using search()
first, and then use one of the specific article titles found.
If the article searched for does not exist, and no similar results exist, NoSuchPageException
will be raised instead.
When requesting the quote of the day, a MissingQOTDException
exception will be raised if the quote of the day could not be extracted from Wikiquote's main page. This usually happens because the page's layout has been changed.
Languages
The wikiquote
module currently supports the following languages:
Language | ISO 639-1 Code |
---|
Basque | eu |
English | en |
French | fr |
German | de |
Hebrew | he |
Italian | it |
Polish | pl |
Portuguese | pt |
Spanish | es |
Use the lang
parameter to specify the language (defaults to en
):
>>> import wikiquote
>>> wikiquote.quotes('Dune', lang='en')[0]
>>> wikiquote.quotes('Victor Hugo', lang='fr')[0]
>>> wikiquote.quotes('Nueve reinas', lang='es')[0]
>>> wikiquote.quote_of_the_day(lang='es')
>>> wikiquote.quotes('Hermann Hesse', lang='de')[0]
>>> wikiquote.quote_of_the_day(lang='it')
>>> wikiquote.quote_of_the_day(lang='pl')
>>> wikiquote.quotes('José Saramago', lang='pt')[0]
Specifying an invalid language will result in an UnsupportedLanguageException
exception.
Tips
Use random.choice()
to select a random quote from an article:
>>> import wikiquote, random
>>> random.choice(wikiquote.quotes('Linus Torvalds'))
Caveats
In some cases, wikiquote
may fail to retrieve quotes from some articles, or the quote of the day (QOTD). This is due to Wikiquote.org's varying internal article layouts: some quotes may be contained in div
elements, others in li
, etc. depending on the article and the language.
Developing
First, make sure you have installed all necessary dependencies, including development dependencies:
$ pip install -r requirements-dev.txt -r requirements.txt
Then, check that all files are correctly formatted, and that the type hints are also correct:
$ make lint
$ make types
After that, check that all tests pass:
$ make test
Some tests may be skipped in the QOTD is not available for some languages.
Finally, create a pull request stating your changes.
Changelog
See the CHANGELOG.md file.
License
The wikiquote
package is licensed under the MIT License.