Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wikipedia API not searching specified term

I’m using the Wikipedia API wrapper for Python, and for some queries, it’s not searching the term I specified. For example, when I execute the function below:

import Wikipedia
wikipedia.summary('machine learning')

I get the error

PageError                                 Traceback (most recent call last)
Cell In[28], line 1
----> 1 wikipedia.summary('machine learning')

File /data/123/anaconda3/envs/comet/lib/python3.8/site-packages/wikipedia/util.py:28, in cache.__call__(self, *args, **kwargs)
     26   ret = self._cache[key]
     27 else:
---> 28   ret = self._cache[key] = self.fn(*args, **kwargs)
     30 return ret

File /data/123/anaconda3/envs/comet/lib/python3.8/site-packages/wikipedia/wikipedia.py:231, in summary(title, sentences, chars, auto_suggest, redirect)
    216 '''
    217 Plain text summary of the page.
    218 
   (...)
    226 * redirect - allow redirection without raising RedirectError
    227 '''
    229 # use auto_suggest and redirect to get the correct article
    230 # also, use page's error checking to raise DisambiguationError if necessary
--> 231 page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
    232 title = page_info.title
    233 pageid = page_info.pageid

File /data/123/anaconda3/envs/comet/lib/python3.8/site-packages/wikipedia/wikipedia.py:276, in page(title, pageid, auto_suggest, redirect, preload)
...
--> 345     raise PageError(self.title)
    346   else:
    347     raise PageError(pageid=self.pageid)

PageError: Page id "machine ;earning" does not match any pages. Try another id!

Does anyone know why this happens?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The module seems to be broken: https://github.com/goldsmith/Wikipedia/issues/279, namely this line in the source code:

title = suggestion or results[0]

It prioritises suggestions over actual result, so it may fail depending on whatever suggestion contains.

Turn of autosuggestions with auto_suggest = False parameter to prevent this.

import wikipedia
print(wikipedia.summary('machine learning', auto_suggest = False))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading