Python 维基百科 API 中的 DisambiguationError 和 GuessedAtParserWarning [英] DisambiguationError and GuessedAtParserWarning in Wikipedia API in Python

查看:30
本文介绍了Python 维基百科 API 中的 DisambiguationError 和 GuessedAtParserWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get a list of possible and acceptable names that Wikipedia has related to the search term. "Corona" in this case.
When typing this:
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

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

Followed by more of these names.

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

I want just the list of the names and not the GuessedAtParserWarning or the DisambiguationError

如何实现?

推荐答案

首先,使用 pip install wikipedia 从 PyPI 安装模块.一旦安装模块 import wikipedia然后 print(wikipedia.summary("Corona"))

First, install the module from the PyPI using pip install wikipedia. Once installed the module import wikipedia Then print(wikipedia.summary("Corona"))

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

If you have fulfilled the above requirements and still the error happens do the following

import warnings

warnings.catch_warnings()

warnings.simplefilter("ignore")

仍然发生错误使用异常处理:

still, the error has occurred use exception handling:

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

这篇关于Python 维基百科 API 中的 DisambiguationError 和 GuessedAtParserWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆