Wikipedia API not searching specified term
Question:
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?
Answers:
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))
If you want to use auto suggest, it works if you don’t use spaces
import wikipedia
wikipedia.summary("machinelearning")
Probably some bug in the package.
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?
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))
If you want to use auto suggest, it works if you don’t use spaces
import wikipedia
wikipedia.summary("machinelearning")
Probably some bug in the package.