I try to display page coordinates. However, some pages do not have coordinates and the API then acts funny.
For:
from wikipedia import wikipedia, DisambiguationError
try:
page = wikipedia.page("West Side Highway")
except DisambiguationError as exception:
page = wikipedia.page(exception.options[0], auto_suggest=False)
when I try to validate if there are some coordinates:
if page.coordinates is None:
print("no coordinates")
I keep getting the KeyError from the wikipedia lib:
if page.coordinates is None:
^^^^^^^^^^^^^^^^
File "/Users/mav/.pyenv/versions/stories_env/lib/python3.11/site-packages/wikipedia/wikipedia.py", line 570, in coordinates
coordinates = request['query']['pages'][self.pageid]['coordinates']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'coordinates'
How can I catch this lack of coordinates for a page to handle it?
The error seems to be from the library itself. According to this GitHub issue, the library doesn't do anything to prevent the KeyError. It's up to you to use try/except to guard against the error. For example: