- If the search term returns multiple pages with similar or related content, the Wikipedia module may raise a DisambiguationError exception. In this case, you can catch the exception and use the options provided in the exception to determine which page to retrieve.
- Here's an example of how you can catch the DisambiguationError exception and select the best page from the options:
- import wikipedia
- try:
- # Search for pages that match the search term
- results = wikipedia.search("search term")
- # Get the best page from the search results
- page = wikipedia.page(results[0])
- except wikipedia.DisambiguationError as e:
- # Print the options provided in the exception
- print("Multiple pages were found with the search term:")
- print(e.options)
- # Prompt the user to select the best page
- selection = input("Please select the best page: ")
- # Get the page for the selected option
- page = wikipedia.page(selection)
- # Print the page's title and summary
- print(page.title)
- print(page.summary)
- This code will search for pages that match the search term, and if multiple pages are found, it will catch the DisambiguationError exception and print the options provided in the exception. The user can then select the best page from the options, and the code will retrieve the selected page and print its title and summary.
- You can also use the options provided in the DisambiguationError exception to automatically select the best page, rather than prompting the user to make a selection. For example, you could use a simple heuristic to select the page with the shortest title or the page with the most related terms to the search term.
- ---
- Sample error output:
- In [18]: get_page("DisambiguationError")
- /home/codespace/venv/lib/python3.8/site-packages/wikipedia/wikipedia.py:389: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
- The code that caused this warning is on line 389 of the file /home/codespace/venv/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.
- lis = BeautifulSoup(html).find_all('li')
- ---------------------------------------------------------------------------
- DisambiguationError Traceback (most recent call last)
- Cell In[18], line 1
- ----> 1 get_page("DisambiguationError")
- File /workspaces/devops_test/mylib/wiki.py:14, in get_page(search)
- 11 results = wikipedia.search(search)
- 13 # Get the best page from the search results
- ---> 14 page = wikipedia.page(results[0])
- 16 # Print the page's title and summary
- 17 print(page.title)
- File ~/venv/lib/python3.8/site-packages/wikipedia/wikipedia.py:276, in page(title, pageid, auto_suggest, redirect, preload)
- 273 except IndexError:
- 274 # if there is no suggestion or search results, the page doesn't exist
- 275 raise PageError(title)
- --> 276 return WikipediaPage(title, redirect=redirect, preload=preload)
- 277 elif pageid is not None:
- 278 return WikipediaPage(pageid=pageid, preload=preload)
- File ~/venv/lib/python3.8/site-packages/wikipedia/wikipedia.py:299, in WikipediaPage.__init__(self, title, pageid, redirect, preload, original_title)
- 296 else:
- 297 raise ValueError("Either a title or a pageid must be specified")
- --> 299 self.__load(redirect=redirect, preload=preload)
- 301 if preload:
- 302 for prop in ('content', 'summary', 'images', 'references', 'links', 'sections'):
- File ~/venv/lib/python3.8/site-packages/wikipedia/wikipedia.py:393, in WikipediaPage.__load(self, redirect, preload)
- 390 filtered_lis = [li for li in lis if not 'tocsection' in ''.join(li.get('class', []))]
- 391 may_refer_to = [li.a.get_text() for li in filtered_lis if li.a]
- --> 393 raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)
- 395 else:
- 396 self.pageid = pageid
- DisambiguationError: "Error (disambiguation)" may refer to:
- Error (band)
- Error (Error EP)
- Errors (band)
- "Error" (song)
- Error (VIXX EP)
- Error (The Warning album)
- Error (Lee Chan-hyuk album)
- Ohms
- Susumu Hirasawa
- Error (Hong Kong group)
- Error (baseball)
- Error (law)
- Error message
- Error (linguistics)
- Errors and residuals
- Error term
- Err (disambiguation)
- I am Error
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license