wikipedia Python Library

repository·master·Indexed 25 days ago

https://github.com/goldsmith/wikipedia

A Python library for accessing and parsing Wikipedia data by wrapping the MediaWiki API. It provides functionality to search for pages, retrieve summaries, access full page content via WikipediaPage objects, perform geographic searches, and manage language settings and rate limiting. Compatible with Python 2.6+ and 3.3+.

Tokens
1.5K
Snippets
7
Records
22
Agent score
82%

What's inside wikipedia

  1. Run tests for the wikipedia library

    master

    To run the test suite, clone the repository and follow these steps in the root directory:

    1. Install requirements.
    2. Run the runtests bash script (covers both Python 2 and 3) or use unittest discover manually.
    $ pip install -r requirements.txt
    $ bash runtests
    # OR manual style:
    $ python -m unittest discover tests/ '*test.py'
  2. Handle DisambiguationError and PageError

    master

    When using wikipedia.summary, be prepared to handle two specific exceptions:

    • wikipedia.exceptions.DisambiguationError: Raised when the query matches multiple distinct pages. You can access the options attribute on the exception object to see the possible matches.
    • wikipedia.exceptions.PageError: Raised when the query does not match any Wikipedia pages.
  3. Get Wikipedia article summaries

    master

    Use the wikipedia.summary() function to retrieve a brief summary of a Wikipedia article by providing the article title. This is useful for quickly getting the introductory text of a page.

    import wikipedia
    
    print wikipedia.summary("Wikipedia")
    # Output: Wikipedia (/ˌwɪkɨˈpiːdiə/ or /ˌwɪkiˈpiːdiə/ WIK-i-PEE-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation...
  4. Change the Wikipedia language

    master

    Use wikipedia.set_lang(lang_code) to switch the language of the Wikipedia data being accessed (e.g., 'fr' for French).

    import wikipedia
    
    wikipedia.set_lang("fr")
    # Subsequent calls will now use the French Wikipedia