python wikipedia package changing input

Viewed 60

I'm running a script to get pages related to a word using python (pip3 install wikipedia). I enter a word to search, let's say the word is "cat". I send that to the code below, but the wikipedia code changes it to "hat" and returns pages related to "hat". It does this with any word I search for (ie: "bear" becomes "beard". "dog" becomes "do", etc...)

wikipedia_page_name = "cat"
print("Original: ", wikipedia_page_name)
myString = wikipedia.page(wikipedia_page_name)
print("Returned: ", myString)

Here is what I get back:

Original:  cat
Returned:  <WikipediaPage 'Hat'>

My steps to use this were to install wikipedia "pip3 install wikipedia" and then import it "import wikipedia". That's it! I've tried uninstalling and then reinstalling, but I get the same results.

Any help is appreciated!

1 Answers

If you want to work with the page <WikipediaPage 'Cat'>, please try to set auto_suggest to False as suggest can be pretty bad at finding the right page:

import wikipedia

wikipedia_page_name = "cat"
print("Original: ", wikipedia_page_name)
myString = wikipedia.page(wikipedia_page_name, pageid=None, auto_suggest=False)
print("Returned: ", myString)

Output:

Original:  cat
Returned:  <WikipediaPage 'Cat'>

If you want to find titles, use search instead:

import wikipedia

wikipedia_page_name = "cat"
searches = wikipedia.search(wikipedia_page_name)
print(searches)

Output:

['Cat', 'Cat (disambiguation)', 'Keyboard Cat', 'Calico cat', 'Pussy Cat Pussy Cat', 'Felidae', "Schrödinger's cat", 'Tabby cat', 'Bengal cat', 'Sphynx cat']

You can use both together to make sure you get the right page from a String, as such:

import wikipedia

wikipedia_page_name = "cat"
searches = wikipedia.search(wikipedia_page_name)
if searches:
    my_page = wikipedia.page(searches[0], pageid=None, auto_suggest=False)
    print(my_page)
else:
    print("No page found for the String", wikipedia_page_name)

Output:

<WikipediaPage 'Cat'>
Related