维基百科 API 中 Python 中的 DisambiguationError 和 GuessedAtParserWarning

DisambiguationError and GuessedAtParserWarning in Wikipedia API in Python

我想获取维基百科与搜索词相关的可能且可接受的名称列表。在这种情况下是“电晕”。
输入时:
print(wikipedia.summary("Corona"))

这给出了输出:

/home/virej/.local/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 ("lxml"). 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/virej/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.

  lis = BeautifulSoup(html).find_all('li')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/virej/.local/lib/python3.8/site-packages/wikipedia/util.py", line 28, in __call__
    ret = self._cache[key] = self.fn(*args, **kwargs)
  File "/home/virej/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 231, in summary
    page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
  File "/home/virej/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 276, in page
    return WikipediaPage(title, redirect=redirect, preload=preload)
  File "/home/virej/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 299, in __init__
    self.__load(redirect=redirect, preload=preload)
  File "/home/virej/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 393, in __load
    raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)
wikipedia.exceptions.DisambiguationError: "Corona" may refer to: 
Stellar corona
Severe acute respiratory syndrome coronavirus 2
Coronavirus disease 2019
COVID-19 pandemic
Corona (beer)
cornice
The Corona, Canterbury Cathedral
Corona (fictional world)
Doraemon: Nobita to Mittsu no Seireiseki
Spider Riders
Tangled
Super Mario Sunshine
Corona (novel)
Paul Celan
Samuel R. Delany
Corona (band)
cantus

后面还有更多这样的名字。

我只想要姓名列表,而不是 GuessedAtParserWarningDisambiguationError

如何实现?

首先,使用 pip install wikipedia 从 PyPI 安装模块。 安装模块后 import wikipedia 那么print(wikipedia.summary("Corona"))

如果您已满足上述要求,但仍然出现错误,请执行以下操作

import warnings

warnings.catch_warnings()

warnings.simplefilter("ignore")

还是出错了 使用异常处理:

try:
 print(wikipedia.summary("Corona"))
except wikipedia.exceptions.DisambiguationError as e:
  print(e.options)