Program Source Code:
import wikipedia
Ask the user for input
search_term = input(“Enter a term to search on Wikipedia: “)
Search for the term on Wikipedia
try:
wikipedia.set_lang(“en”) # Set the language to English
page = wikipedia.page(search_term)
summary = page.summary
print(summary)
except wikipedia.exceptions.PageError:
print(“Sorry, no page on Wikipedia matches your search term.”)
In this program, we first import the wikipedia
package. We then ask the user for input of a term to search on Wikipedia.
We use a try
block to catch any errors that might occur while searching for the term. We set the language to English using the set_lang
method, then call the page
method with the search term as an argument. This returns a wikipedia.WikipediaPage
object, which we can use to access information about the page, including the summary.
If no page on Wikipedia matches the search term, a wikipedia.exceptions.PageError
will be raised. We catch this error and print a message to the user.
Note that you may need to install the wikipedia
package using pip install wikipedia
if you haven’t already done so.