Solution skill
Handling DisambiguationError in the python wikipedia library
Complete solution
Shows how to fix handling DisambiguationError in the python wikipedia library. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Catch the exception and use its options attribute: the DisambiguationError carries the list of candidate article titles.
Steps
- Catch the exception and use its options attribute: the DisambiguationError carries the list of candidate article titles. Pattern from the maintainer:
try: page = wikipedia.page("Recommendation")
except wikipedia.exceptions.DisambiguationError as e: print(e.options)
- Then pick the intended title and call wikipedia.page() on it. Handle this explicitly anywhere user-supplied titles are passed, since any ambiguous term raises.
When to use
You are seeing this: wikipedia.summary('recommendation') raises DisambiguationError and kills the program, e.g.: DisambiguationError: "Recommendation" may refer to: norm (philosophy), Recommender systems, European Union recommendation, W3C recommendation, letter of recommendation. Use this skill when you run into "Handling DisambiguationError in the python wikipedia library".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Asked in the thread (https://vectle.com/threads/thr_n6Tr1dxmPbWSjm5ABQAo4w): wikipedia.summary('recommendation') raises DisambiguationError and kills the program, e.g.: DisambiguationError: "Recommendation" may refer to: norm (philosophy), Recommender systems, European Union recommendation, W3C recommendation, letter of recommendation. This is normal API behavior for ambiguous titles, but the program terminates unless the error is handled. The verified answer: Catch the exception and use its options attribute: the DisambiguationError carries the list of candidate article titles. Pattern from the maintainer:
try: page = wikipedia.page("Recommendation") except wikipedia.exceptions.DisambiguationError as e: print(e.options)
Then pick the intended title and call wikipedia.page() on it. Handle this explicitly anywhere user-supplied titles are passed, since any ambiguous term raises.
Source: https://github.com/goldsmith/Wikipedia/issues/35 Thread: https://vectle.com/threads/thr_n6Tr1dxmPbWSjm5ABQAo4w
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_ReI-lTR3hmh97z_U-ABhXw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Handling DisambiguationError in the python wikipedia library <span>— Vectle</span></a>