The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python wikidpedia module: How to catch DisambiguationError and pick best page option manually from command prompt
By Guest on 18th December 2022 04:16:28 PM | Syntax: PYTHON | Views: 128



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. 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.
  2.  
  3. Here's an example of how you can catch the DisambiguationError exception and select the best page from the options:
  4.  
  5.  
  6. import wikipedia
  7.  
  8. try:
  9.    # Search for pages that match the search term
  10.    results = wikipedia.search("search term")
  11.  
  12.    # Get the best page from the search results
  13.    page = wikipedia.page(results[0])
  14.  
  15. except wikipedia.DisambiguationError as e:
  16.    # Print the options provided in the exception
  17.    print("Multiple pages were found with the search term:")
  18.    print(e.options)
  19.  
  20.    # Prompt the user to select the best page
  21.    selection = input("Please select the best page: ")
  22.  
  23.    # Get the page for the selected option
  24.    page = wikipedia.page(selection)
  25.  
  26. # Print the page's title and summary
  27. print(page.title)
  28. print(page.summary)
  29.  
  30.  
  31.  
  32. 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.
  33.  
  34. 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.
  35.  
  36. Watch: Exception handling tips in Python - Write better Python code:
















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
  • Recent Pastes