在python中使用Wikipedia导入模块时出现歧义消除错误

如何解决在python中使用Wikipedia导入模块时出现歧义消除错误

在我的代码中使用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,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。 如果可以的话:)

解决方法

因此,我对Wikipedia模块给出的消除歧义错误的了解是,当一个单词表示多于一件事时,它不知道我们的意思。因此,它会产生消歧错误。在您要求摘要之前,建议您先搜索单词。

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."
, 如果没有与我们使用的关键字相关的文章或页面清晰,则会出现

歧义错误。尝试以下代码:

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

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

,

我尝试了一下,但是问题没有解决。我收到和以前一样的错误:(

让我进一步简化查询

我的代码1:

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

执行此操作时,错误如前所述。

我的代码2:

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

当我这样做时,它显示的是Cat而不是Rat的维基百科摘要。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?