Welcome to MediaWikiAPI’s documentation!

Release v.

https://img.shields.io/pypi/v/mediawikiapi.svg https://img.shields.io/pypi/pyversions/mediawikiapi.svg https://github.com/lehinevych/MediaWikiAPI/workflows/Python%20package/badge.svg?branch=master https://img.shields.io/github/issues/lehinevych/MediaWikiAPI.svg https://img.shields.io/badge/license-MIT%20License-brightgreen.svg

Installation

Start using wikipedia for Python in less than 5 minutes! MediaWikiAPI is compatible with Python 3 and Python 2.7. If you are looking for the the full developer API, see MediaWikiAPI Documentation.

Begin by installing wikipedia:

$ pip install mediawikiapi

As alternative you can use the source code from Github.

Quickstart

Now let’s use the MediaWikiAPI. First you need to import the package and create MediaWikiAPI class. In order to use search and suggestion call the corresponding methods search and suggest:

>>> from mediawikiapi import MediaWikiAPI
>>> mediawikiapi = MediaWikiAPI()
>>> mediawikiapi.search("Barack")
      [u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama']

>>> mediawikiapi.suggest("Barak Obama") # returns the suggested Wikipedia title for a query or None
      u'Barack Obama'

We can also get fewer or more results by using the results kwarg:

>>> mediawikiapi.search("Ford", results=3)
[u'Ford Motor Company', u'Gerald Ford', u'Henry Ford']

To get the summary of an article, use mediawikiapi.summary:

>>> mediawikiapi.summary("GitHub")
2011, GitHub was the most popular open source code repository site.\nGitHub Inc. was founded in 2008 and is based in San Francisco, California.\nIn July 2012, the company received $100 million in Series A funding, primarily from Andreessen Horowitz.'

>>> mediawikiapi.summary("Apple III", sentences=1)
u'The Apple III (often rendered as Apple ///) is a business-oriented personal computer produced and released by Apple Computer that was intended as the successor to the Apple II series, but largely considered a failure in the market. '

mediawikiapi.page enables you to load and access data from full Wikipedia pages. Initialize with a page title (keep in mind the errors listed above), and then access most properties using property methods:

>>> ny = mediawikiapi.page("New York (state)")

>>> ny.title
u'New York (state)'

>>> ny.url
u'http://en.wikipedia.org/wiki/New_York_(state)'

>>> ny.content
u'New York is a state in the northeastern United States. New York was one of the original thir'...

>>> ny.images[0]
u'http://upload.wikimedia.org/wikipedia/commons/9/91/New_York_quarter%2C_reverse_side%2C_2001.jpg'

>>> ny.links[0]
u'1790 United States Census'

To change the language of the Wikipedia you are accessing, use mediawikiapi.conf.language. Remember to search for page titles in the language that you have set, not English!:

>>> mediawikiapi.config.language = "fr"

>>> print mediawikiapi.summary("Francois Hollande")
François Hollande, né le 12 août 1954 à Rouen, en Seine-Maritime, est un homme d'État français. Il est président de la République française depuis le 15 mai 2012...

To get a list of all possible language prefixes, try mediawikiapi.languages().

Exception handling example:

>>> import mediawikiapi
>>> mediawiki = mediawikiapi.MediaWikiAPI()
>>> try:
>>>    print("Getting page")
>>>    page = mediawiki.page("!!!123123 page title")
>>> except mediawikiapi.exceptions.PageError:
>>>    print("Got page error, skipping")

The library can be used for other Mediawiki installs and sites with the same API.

>>> import mediawikiapi
>>> mediawiki = mediawikiapi.MediaWikiAPI(config=Config(mediawiki_url="https://{}.wiktionary.org/w/api.php"))

For more details and configuration option check API section.

Changelog

Changelog

Here you can find the full developer API for the MediaWikiAPI project.

Contents:

Version 1.2

  • The Config allows to specify the API url. This makes the client reusable for other Mediawiki installs with the same API.
  • Move to the poetry packaing and dependencies management tool
  • Add typing and mypy check
  • Format the code and use the formatting checks

Version 1.1.6

  • Add support for Python 3.9, 3.10 and 3.11

Version 1.1.5

  • Fix the bug with pip install on Windows
  • Fix setup.py to not fail if file doesn’t exist

Version 1.1.4

  • Split requirements.txt and upgrade dependencies versions.
  • Add support for python 3.7
  • Add infobox property to wikipediapage class

Version 1.1.3

  • Add category member query support by PR #24
  • Add backlinks to WikipediaPage PR #22
  • Fix missing self reference PR #11 by @danielunderwood
  • Fix dependencies installable from setup.py PR #12 by @danielunderwood
  • Fix the hangs of the continuing queries PR #21

Version 1.1

  • Breaking change - add MediaWikiAPI class, now you can have more than one api access point with different configurations (Config instances). MediaWikiAPI class contains all the mediawikiapi function from version 1.0. The Config class cloud be pass as parameter during initialization.
  • Support python 2
  • Rename Configuration class to Config, add language field
  • Config().get_api_url now accept language parameter
  • Add timeout for requests, field in Config class called timeout (in seconds).
  • Makes the pagepropsof a wikipedia page accessible PR #147 from @goldsmith repo.
  • Fix suggestion, issue #108 by PR #131 from @goldsmith repo.
  • Fix problem with hidden files in the article PR #132 @goldsmith repo.
  • DisambiguationError contains now information about title and url PR #92 from @goldsmith repo.
  • Fix issue where pageid request => redirect raises error PR #165

Version 1.0

  • Fork Wikipedia
  • Add language validation for mediawikiapi.set_lang
  • Add lang title method to WikipediaPage
  • Add re-usage the same requests session
  • Fix installing error with version
  • Fix WikipediaPage.sections
  • Fix mock data
  • Refactoring: seperate Language and Configuration classes

Indices and tables

API

MediaWikiAPI Documentation

Here you can find the full developer API for the MediaWikiAPI project.

Contents:

Classes

Exceptions

Indices and tables

Indices and tables