### Install Python Wikipedia Library Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This snippet demonstrates how to install the `wikipedia` Python library using pip, the Python package installer. This is the first step to begin using the library. ```Bash $ pip install wikipedia ``` -------------------------------- ### Perform Wikipedia Search and Suggestion in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This example shows how to use `wikipedia.search` to find Wikipedia articles matching a query and `wikipedia.suggest` to get a suggested Wikipedia title for a query. `suggest` returns `None` if no suggestion is found. ```Python import wikipedia wikipedia.search("Barack") # Expected output: [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'] wikipedia.suggest("Barak Obama") # Expected output: u'Barack Obama' ``` -------------------------------- ### Get Wikipedia Article Summary in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This example shows how to retrieve a summary of a Wikipedia article using `wikipedia.summary`. You can specify the number of sentences to retrieve using the `sentences` keyword argument. ```Python wikipedia.summary("GitHub") # Expected output: 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.' wikipedia.summary("Apple III", sentences=1) # Expected output: 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. ' ``` -------------------------------- ### Load and Access Wikipedia Page Properties in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This example demonstrates how to load a full Wikipedia page using `wikipedia.page` and then access various properties like title, URL, content, images, and links. Errors like `DisambiguationError` or `PageError` can occur during initialization. ```Python ny = wikipedia.page("New York") ny.title # Expected output: u'New York' ny.url # Expected output: u'http://en.wikipedia.org/wiki/NewYork' ny.content # Expected output: u'New York is a state in the Northeastern region of the United States. New York is the 27th-most exten'... ny.images[0] # Expected output: u'http://upload.wikimedia.org/wikipedia/commons/9/91/New_York_quarter%2C_reverse_side%2C_2001.jpg' ny.links[0] # Expected output: u'1790 United States Census' ``` -------------------------------- ### List Available Wikipedia Languages in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This example demonstrates how to check if a specific language is supported and how to retrieve the full name of a language using `wikipedia.languages()`. ```Python 'en' in wikipedia.languages() # Expected output: True print wikipedia.languages()['es'] # Expected output: español ``` -------------------------------- ### Build Local Documentation for Wikipedia Library Source: https://github.com/goldsmith/wikipedia/blob/master/README.rst This sequence of commands shows how to install Sphinx and then build the project's HTML documentation locally. This is useful for developers who want to view or contribute to the documentation. ```bash $ pip install sphinx $ cd docs/ $ make html ``` -------------------------------- ### Install Wikipedia Python Library via Pip Source: https://github.com/goldsmith/wikipedia/blob/master/README.rst This command demonstrates how to install the Wikipedia Python library using the `pip` package manager. It ensures the library and its dependencies are available in your Python environment. ```bash $ pip install wikipedia ``` -------------------------------- ### Limit Wikipedia Search Results in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This snippet demonstrates how to control the number of results returned by `wikipedia.search` using the `results` keyword argument. This allows for more focused searches. ```Python wikipedia.search("Ford", results=3) # Expected output: [u'Ford Motor Company', u'Gerald Ford', u'Henry Ford'] ``` -------------------------------- ### Open Wikimedia Donation Page in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This snippet shows how to use `wikipedia.donate()` to open the Wikimedia project's donation page in the default web browser. This function acknowledges the importance of Wikimedia for the library's functionality. ```Python wikipedia.donate() # Expected behavior: # your favorite web browser will open to the donations page of the Wikimedia project # because without them, none of this would be possible ``` -------------------------------- ### Run Local Tests for Wikipedia Python Library Source: https://github.com/goldsmith/wikipedia/blob/master/README.rst These commands outline the process for setting up the test environment and executing tests for the Wikipedia Python library. It includes installing test dependencies and running tests using both a bash script and Python's `unittest` module. ```bash $ pip install -r requirements.txt $ bash runtests # will run tests for python and python3 $ python -m unittest discover tests/ '*test.py' # manual style ``` -------------------------------- ### Change Wikipedia Access Language in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This snippet shows how to change the language of the Wikipedia content being accessed using `wikipedia.set_lang`. It's important to search for page titles in the newly set language. ```Python wikipedia.set_lang("fr") print wikipedia.summary("Francois Hollande") # Expected output: 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... ``` -------------------------------- ### Handle Wikipedia Summary Errors in Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/quickstart.rst This snippet illustrates how `wikipedia.summary` can raise `DisambiguationError` for ambiguous queries or `PageError` if a page doesn't exist. It also shows how to catch `DisambiguationError` and access the `options` attribute to see possible disambiguation choices. ```Python wikipedia.summary("Mercury") # Expected output (Traceback): # Traceback (most recent call last): # ... # wikipedia.exceptions.DisambiguationError: "Mercury" may refer to: # Mercury (mythology) # Mercury (planet) # Mercury (element) try: mercury = wikipedia.summary("Mercury") except wikipedia.exceptions.DisambiguationError as e: print e.options # Expected output: [u'Mercury (mythology)', u'Mercury (planet)', u'Mercury (element)', u'Mercury, Nevada', ...] wikipedia.summary("zvv") # Expected output (Traceback): # Traceback (most recent call last): # ... # wikipedia.exceptions.PageError: "zvv" does not match any pages. Try another query! ``` -------------------------------- ### Get Wikipedia article summary using Python Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/index.rst This snippet demonstrates how to import the `wikipedia` library and retrieve a summary of a Wikipedia article. It shows a basic usage of the `summary` function to get a concise overview of a given topic. ```Python import wikipedia print wikipedia.summary("Wikipedia") # 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... ``` -------------------------------- ### Wikipedia Module Core Functions Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/code.rst Documents the main functions available directly from the `wikipedia` module for interacting with Wikipedia, including search, suggestion, summary, page retrieval, and geographical search. ```APIDOC wikipedia.search(query: str, results: int = 10, suggestion: bool = False) query: The search query string. results: The maximum number of results to return (default: 10). suggestion: If True, include a spelling suggestion (default: False). wikipedia.suggest(query: str) query: The query string for which to get a suggestion. wikipedia.summary(query: str, sentences: int = 0, chars: int = 0, auto_suggest: bool = True, redirect: bool = True) query: The query string for the summary. sentences: The number of sentences to return (default: 0, returns full summary). chars: The number of characters to return (default: 0, returns full summary). auto_suggest: If True, auto-suggest a title if the query is not exact (default: True). redirect: If True, follow redirects (default: True). wikipedia.page(title: str = None, pageid: int = None, auto_suggest: bool = True, redirect: bool = True, preload: bool = False) title: The title of the Wikipedia page. pageid: The page ID of the Wikipedia page. auto_suggest: If True, auto-suggest a title if the query is not exact (default: True). redirect: If True, follow redirects (default: True). preload: If True, pre-load content, categories, references, etc. (default: False). wikipedia.geosearch(latitude: float, longitude: float, title: str = None, results: int = 10, radius: int = 1000) latitude: The latitude for the geographical search. longitude: The longitude for the geographical search. title: An optional title to search near. results: The maximum number of results to return (default: 10). radius: The search radius in meters (default: 1000). ``` -------------------------------- ### Wikipedia Utility and Configuration Functions Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/code.rst Documents utility functions for managing language settings, rate limiting, and accessing random or donation information within the Wikipedia library. ```APIDOC wikipedia.languages() -> dict[str, str] Returns a dictionary of language codes and their names supported by Wikipedia. wikipedia.set_lang(language_code: str) language_code: The language code (e.g., 'en', 'es') to set for future requests. wikipedia.set_rate_limiting(rate_limit: bool = True, min_wait: float = 0.05) rate_limit: If True, enable rate limiting (default: True). min_wait: The minimum time to wait between requests in seconds (default: 0.05). wikipedia.random(pages: int = 1) -> str | list[str] pages: The number of random page titles to return (default: 1). Returns a single title string or a list of title strings. wikipedia.donate() Opens the Wikimedia Foundation donate page in your browser. ``` -------------------------------- ### Jinja2 Sphinx Layout Customization Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/_themes/flask_small/layout.html This snippet demonstrates how to extend a base Sphinx layout using Jinja2 templating. It overrides header, footer, and sidebar blocks, conditionally displays content based on the page name, and integrates a GitHub 'Fork me' ribbon using a theme variable. ```Jinja2 {% extends "basic/layout.html" %} {% block header %} {{ super() }} {% if pagename == 'index' %} {% endif %} {% endblock %} {% block footer %} {% if pagename == 'index' %} {% endif %} {% endblock %} {# do not display relbars #} {% block relbar1 %}{% endblock %} {% block relbar2 %} {% if theme_github_fork %} [![Fork me on GitHub](http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png)](http://github.com/{{ theme_github_fork }}) {% endif %} {% endblock %} {% block sidebar1 %}{% endblock %} {% block sidebar2 %}{% endblock %} ``` -------------------------------- ### Wikipedia Library Exceptions Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/code.rst Lists the custom exception classes defined within the `wikipedia.exceptions` module, used to signal specific error conditions during API interactions. ```APIDOC wikipedia.exceptions.PageError(pageid: str = None, title: str = None) Raised when a page does not exist. pageid: The ID of the page that caused the error. title: The title of the page that caused the error. wikipedia.exceptions.DisambiguationError(title: str, may_refer_to: list[str]) Raised when a query resolves to a disambiguation page. title: The title of the disambiguation page. may_refer_to: A list of possible titles the query may refer to. wikipedia.exceptions.RedirectError(title: str) Raised when a page redirects to another page and redirect=False. title: The title of the page that caused the redirect error. wikipedia.exceptions.HTTPTimeoutError(query: str) Raised when an HTTP request times out. query: The query that caused the timeout. wikipedia.exceptions.WikipediaException(message: str) Base exception class for all wikipedia library errors. message: The error message. ``` -------------------------------- ### WikipediaPage Class Reference Source: https://github.com/goldsmith/wikipedia/blob/master/docs/source/code.rst Details the `WikipediaPage` class, which represents a Wikipedia page and provides methods to access its content, categories, references, and other properties. ```APIDOC class wikipedia.WikipediaPage __init__(title: str = None, pageid: int = None, redirect: bool = True, preload: bool = False, original_title: str = '') title: The title of the Wikipedia page. pageid: The page ID of the Wikipedia page. redirect: If True, follow redirects (default: True). preload: If True, pre-load content, categories, references, etc. (default: False). original_title: The original title before any redirects. title: str url: str content: str summary: str images: list[str] links: list[str] categories: list[str] references: list[str] html: str coordinates: tuple[float, float] pageid: str parent_id: str revision_id: str sections: list[str] section(section_title: str) -> str section_title: The title of the section to retrieve. ``` -------------------------------- ### Accessing Wikipedia Data with Python Library Source: https://github.com/goldsmith/wikipedia/blob/master/README.rst This snippet demonstrates core functionalities of the `wikipedia` Python library, including retrieving article summaries, performing searches, accessing page object attributes (title, URL, content, links), and setting the query language. It showcases how to interact with Wikipedia data programmatically. ```python >>> import wikipedia >>> print wikipedia.summary("Wikipedia") # 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... >>> wikipedia.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'] >>> ny = wikipedia.page("New York") >>> ny.title # u'New York' >>> ny.url # u'http://en.wikipedia.org/wiki/New_York' >>> ny.content # u'New York is a state in the Northeastern region of the United States. New York is the 27th-most exten'... >>> ny.links[0] # u'1790 United States Census' >>> wikipedia.set_lang("fr") >>> wikipedia.summary("Facebook", sentences=1) # Facebook est un service de réseautage social en ligne sur Internet permettant d'y publier des informations (photographies, liens, textes, etc.) en contrôlant leur visibilité par différentes catégories de personnes. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.