Related Tags

python
communitycreator

# How to use wikipedia.search() in Python

Harsh Jain

If you are creating an application that requires you to fetch the search results for a particular term from Wikipedia, what approach would you use?

Maybe you would use the requests module or BeautifulSoup. However, getting the desired data in the proper format could be a tedious job.

The Wikipedia package can help you fetch the search results from Wikipedia with just one line of code. Before moving on, let’s first install the package by running the following command:

pip install wikipedia


Let’s now take a look at the function parameters.

## Parameters for wikipedia.search()

wikipedia.search() accepts the following parameters:

• query: This is the term that you want to search.

• results: This is an optional parameter that specifies how many results you want for the query. It accepts an integer value. The default value of this parameter is $10$.

• suggestion: This is also an optional parameter that accepts boolean values. If True, it will return suggestions based on your query. The default value of this parameter is False.

## Returns

The search() function returns a list of available terms on Wikipedia for your query.

Let’s take a look at the code:

import wikipedia as wp

query = "Python"
print(wp.search(query, results = 5))
Use wikipedia.search() function in Python

Explanation:

• In line 1, we import the required package.
• In line 3, we define our search term.
• In line 4, we use the search() function and pass the required parameters.
• In the output, we can see the search results that are available on Wikipedia for our query.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Harsh Jain
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time