2

在我的代码中使用 Wikipedia 模块时,出现消歧错误。

**我的代码:

import wikipedia
print("Using Wikipedia")
while True:
     input = input("Question: ")
     print(wikipedia.summary(input))

**输出:

Using Wikipedia
Question:

在此之前一切正常,但是当用户提出问题时,似乎有问题。大多数问题都没有问题,但是当问题是“狗”时,会弹出一个错误。(这个问题也出现在几个问题上,不仅限于狗)

**错误:

/home/ameya/.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/ameya/.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 "main.py", line 10, in <module>
print(wikipedia.summary(input))
File "/home/ameya/.local/lib/python3.8/
site-packages/wikipedia/util.py", line 28, in __call__
ret = self._cache[key] = self.fn(*args, **kwargs)
File "/home/ameya/.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/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 276, in page
return WikipediaPage(title, redirect=redirect, preload=preload)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 299, in __init__
self.__load(redirect=redirect, preload=preload)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 393, in __load
raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)

这是它建议的解决方案:

The code that caused this warning is on line 389 of the file 
/home/ameya/.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."

但是,即使这样做了,问题也没有解决。

另外,一件重要的事情,我使用的操作系统是 Ubuntu。如果这有帮助:)

4

3 回答 3

1

所以我开始知道维基百科模块给出的消歧错误是,当一个词的意思不止一件时,它不知道我们的意思。因此,它会引发消歧错误。在您要求摘要之前,我建议您搜索该词。

import wikipedia
print(wikipedia.search('dog'))

输出:

['Dog', 'Dog (disambiguation)', 'The Dog', 'Dog Eat Dog', 'That Dog', 'Hot dog', 'Dog Bite Dog', 'Difference of Gaussians', 'Duane Chapman', 'German Shepherd']

现在'The Dog'从输出中使用,这里建议不要选择'Dog'哪个也是很多词的一部分

print(wikipedia.summary("The Dog"))

输出:

The dog (Canis familiaris when considered a distinct species or Canis lupus familiaris when considered a subspecies of the wolf) is a domesticated carnivore of the family Canidae. It is part of the wolf-like canids, and is the most widely abundant terrestrial carnivore. The dog and the extant gray wolf are sister taxa as modern wolves are not closely related to the wolves that were first domesticated, which implies that the direct ancestor of the dog is extinct. The dog was the first species to be domesticated, and has been selectively bred over millennia for various behaviors, sensory capabilities, and physical attributes.Their long association with humans has led dogs to be uniquely attuned to human behavior, and they can thrive on a starch-rich diet that would be inadequate for other canids. Dogs vary widely in shape, size, and colors. They perform many roles for humans, such as hunting, herding, pulling loads, protection, assisting police and military, companionship, and, more recently, aiding disabled people, and therapeutic roles. This influence on human society has given them the sobriquet of "man's best friend."
于 2020-10-14T04:41:12.087 回答
0

当没有与我们正在使用的关键字相关的文章或页面是明确的时,会引发歧义错误。试试这个代码:

try:
p = wikipedia.page('dog')
except wikipedia.DisambiguationError as e:
print(e.options)

来源:https ://wikipedia.readthedocs.io/en/latest/quickstart.html

于 2020-10-13T07:07:15.260 回答
0

我试过了,但问题没有解决。我得到与以前相同的错误:(

让我进一步简化我的查询,

我的代码 1:

import Wikipedia 
print(wikipedia.summary("Dog"))

当我这样做时,错误如前所述。然而,

我的代码 2:

import Wikipedia
print(wikipedia.summary("Rat"))

当我这样做时,它显示了 Cat 而不是 Rat 的 Wikipedia 摘要。

因此,似乎存在一些常见错误,因此无法获得正确的摘要。

于 2020-10-13T12:15:47.707 回答